how to pickle a lambda?

Erik Max Francis max at alcyone.com
Sun Apr 28 18:09:14 EDT 2002


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.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Life is one long process of getting tired.
\__/ Samuel Butler
    Interstelen / http://www.interstelen.com/
 A multiplayer, strategic, turn-based Web game on an interstellar scale.



More information about the Python-list mailing list