problem pickling a function
Calvin Spealman
ironfroggy at socialserve.com
Wed Dec 12 11:48:15 EST 2007
On Dec 12, 2007, at 11:01 AM, Emin.shopper Martinian.shopper wrote:
> Dear Experts,
>
> I love the pickle module, but I occasionally have problems pickling
> a function. For example, if I create an instance g of class f and
> assign g.x to a function, then I cannot pickle g (example code
> below). I know that I can pickle f separately if I want to, and I
> understand why I get the pickling error.
This all has to do with how pickles are able to recreate the objects
in question. Pickle can only handle things, basically, with global
names. This pretty much means it can handle regular classes and
module functions, but not the methods of those classes. Why? Why
would it have to? If it can handle the class it just expects that
name to be the same class when it brings it back up, it doesn't
actually store the class or function itself, just the name.
> But is there a way to assign functions to instances of a class
> without preventing pickleability? It doesn't seem unreasonable to
> me to want to assign functions to instances of a class (after all
> functions are first class objects, so why shouldn't I be able to
> pass them around?) Is there a better way or is this just a
> limitation of pickle?
Presumably you could do something with __getstate__ and __setstate__
methods, but this is many cases of "Are you really sure you want to
do that?"
More information about the Python-list
mailing list