Why can't instance methods be pickled?

Fredrik Lundh effbot at telia.com
Sun Oct 8 10:23:48 EDT 2000


Nicholas wrote:
> Is there a fundamental reason why instance method pickling is not
> implemented?

quoting the pickle docs:

    http://www.python.org/doc/current/lib/module-pickle.html

    "Note that when class instances are pickled, their class's code
    and data are not pickled along with them. Only the instance
    data are pickled. This is done on purpose, so you can fix bugs
    in a class or add methods and still load objects that were
    created with an earlier version of the class."

> Has anyone written code to solve this problem?

What do you want pickle to do in this case?

If you want to pickle the actual code, you can register a method
pickler using copy_reg (use marshal to serialize the code object).

If you want to ignore the function pointer, implement __getstate__
and __setstate__ methods in your class (see the pickle docs for
details).

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list