right now I'm using closures to generate functions like in this simplified example:
def constant_function(constant):
def dummyfunction(t):
return constant
return dummyfunction
These generated functions are then passed to the init-method of a custom class which stores them as instance attributes. The disadvantage is that that makes the class-instances unpickleable. So I'm wondering if there is a way to create function generators avoiding closures.
You could use a callable class:
class ConstantFunction(object):
def __init__(self, constant):
self.constant = constant
def __call__(self, t):
return self.constant
def constant_function(constant):
return ConstantFunction(constant)
The closure state of your function is then transferred to an instance attribute instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With