понеделник, 27 юли 2015 г.

Universal function interface in Python language for reusable and compatible code.

When I started to hack in Python after Java thinking there were a need for defining  data and /or function interface to pass over other functions, defining API, etc. I was missing that design time frame-working in source coded interfaces and/or inheritances was needing re-factoring each time when interface changed. And big thinking was... wtf is the best interface for my API. But it has always being there ... just in front me but it was kept hidden for a while until this obvious axiom came into my mind:
Every Python function object has None arguments, arguments tuple(), keyword arguments dict() or both args and kwargs.  
The obvious answer to which is the universal function interface: ( *args,  **kwargs ) is python native protocol to handle any kind of arguments, if you do type - kind understanding of what comes around. ( it should be OK to do that in your specific implementation). Also one great feature from Python as platform are that things there are dictionary items, and this make it's use extremely flexible and productive.

Other obvious  standard interface: the dict: __setitem__(self, key, value),  __getitem__(self, key) , __delitem__(self, key) ,  or d[ 'key' ] = 'value', d['key'], del d['key'] , here language itself define a slight setter and getter interface for key value storage ( associative array, map ) There are also list() interface defining behavior for stack, queue, linked-list, and many-many of modules defining any different kind of different interfaces and flavors to dictionaries and lists, that are unable to bound to standard predefined interfaces.

For that reason my conclusion is that if I want python code to be implementation independent as reusable plugin module, I should use ( <self>, *args, **kwargs ) as universal and native pythonic interface even in well defined standard interface as dict. Also ( <self>,*args ) if  I want to translate easily generic Python prototype code into C implementation for achieving high performance component. 

Няма коментари:

Публикуване на коментар