how to pickle a lambda?
Alejandro Dubrovsky
s328940 at student.uq.edu.au
Mon Apr 29 06:32:13 EDT 2002
On Mon, 29 Apr 2002 08:09:14 +1000, Erik Max Francis wrote:
> Alejandro Dubrovsky wrote:
>
>> i've got a class that defines something like the following:
>>
>> def t(self,a,b):
>> ...
>>
>> def h(self):
>> self.thelambdafunctiontobepickled = lambda x: self.t(x,4)
>>
>> is there any simple way of pickling that lambda function above so that
>> when i unpickle it will be set to the right function?
>
> You can't pickle a lambda function. However, you could for instance
> pickle a string representation of it that when eval'd gives the desired
> lambda.
yes, but that means saving the definition as a string everytime that the
lambda is changed. i was looking for something "neater". eg for function
variables, i can just save and restore the variables'.__name__ attribute.
i was wondering if there was something similar for lambdas.
Thanks,
Alejandro
More information about the Python-list
mailing list