Would like to define something I'd best call 'function extension' / 'function interface' or 'class of functions' in Python. Haven't seen similar constructs in other languages, but I'm no expert in functional languages like LISP. Considering to change to other language later if this is easier to do, say in Julia.
'Function extension' would be a formal way to express that binding few arguments of a function, we end up with a function belonging to a specific 'class of functions'. In my examples below the specific 'class of functions' is a data transformation function (as transform_data_func_class
), which has a sole argument: data
. power_data
extends transform_data_func_class
as binding the exponent
argument we end up with a function which belongs to the the 'class of functions' transform_data_func_class
.
A 'class of functions' defines part of the signature like:
def transform_data_func_class(data):
raise InterfaceError('Called unimplemented function class.')
Then would define functions extending the 'class of functions':
def power_data(data, exponent): # extends: transform_data_func_class
return data ** exponent # apply the additional the parameter
So that when binding the additional parameter(s) we end up with a signature matching that of the extended 'class of functions':
square = partial(power_data, exponent=2) # matches signature transform_data_func_class(data)
I'll use these for example, in order to specify types of functions as inputs and outputs of other functions:
def chain(data, transform_data_func_class_list):
"""Passing in data and a list of functions being elements of
transform_data_func_class with a sole argument: data."""
...
Find it odd that while in the case of general objects it is relatively easy to define classes/types, in case of functions there is no such obvious construct. Currently I use extends: function_class
comments to specify function types/classes - an idea borrowed from mypy
- ideally would use something more formal.
Any suggestions welcome, would be great if I could just call the resulting construct through __call__
. Have thought of a class like:
class TransformDataFuncClass(object):
@classmethod
def exec(cls, data):
raise InterfaceError('Called unimplemented function class.')
But don't like the ununtiutive exec() call. Have also thought of:
class TransformDataFuncClass(object):
@classmethod
def __call__(cls, data):
raise InterfaceError('Called unimplemented function class.')
but I get TypeError: object() takes no parameters
when calling TransformDataFuncClass(data=1)
. Signaling 'function extension' through class inheritence would be a great way though.
My most elaborate plan was to do:
class FuncClass(object):
"""Takes care of function classes and extensions being
just callable through __call__, e.g. PowerData(data=1, power=2)
instead of instantiating first: PowerData()(data=1, power=2)"""
def __init__(self, wrapped_class):
self.wrapped_class = wrapped_class
self.wrapped_instance = wrapped_class()
def __call__(self, *args, **kwargs):
return self.wrapped_instance(*args, **kwargs)
@FuncClass
class TransformDataFuncClass(object):
def __call__(self, data):
raise InterfaceError('Called unimplemented function class.')
@FuncClass
class PowerData(TransformDataFuncClass.wrapped_class):
def __call__(self, data, exponent):
return data ** exponent # apply the additional the parameter
So that TransformDataFuncClass(data=1)
and PowerData(data=1, exponent=2)
are natural and valid calls. While the latter actually worked its a bit complicated, and couldn't parallelize calculations as I got errors from dill
(an smarter alternative to pickle
) saying that <class PowerData>: it's not the same object as PowerData
.
Any suggestions welcome, really. I'm also interested in theoretical considerations and how this is done in other languages.