[Tutor] Unpickling a Pickled Object

Andrew Wilkins toodles@yifan.net
Fri, 4 May 2001 16:10:26 +0800


Hi Jethro,

I'll just post what it says in the documentation:

"""
When a pickled class instance is unpickled, its __init__() method is
normally not invoked. Note: This is a deviation from previous versions of
this module; the change was introduced in Python 1.5b2. The reason for the
change is that in many cases it is desirable to have a constructor that
requires arguments; it is a (minor) nuisance to have to provide a
__getinitargs__() method.

If it is desirable that the __init__() method be called on unpickling, a
class can define a method __getinitargs__(), which should return a tuple
containing the arguments to be passed to the class constructor (__init__()).
This method is called at pickle time; the tuple it returns is incorporated
in the pickle for the instance.
"""

I hope that helps =)

Andrew

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> Jethro Cramp
> Sent: Saturday, 5 May 2001 6:13 AM
> To: Tutor@python.org
> Subject: [Tutor] Unpickling a Pickled Object
>
>
> When a pickled object is unpickled does __init__  get called. I can
> think of half a dozen reasons why it shouldn't, but just thought I'd ask.
>
> TIA,
>
> Jethro
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>