Pickle, __init__, and classes

Russell Blau russblau at hotmail.com
Tue Aug 2 16:27:44 EDT 2005


"Yin" <yin_12180 at yahoo.com> wrote in message
news:1123013424.847230.300030 at g47g2000cwa.googlegroups.com...
> I've created a class that reads in and processes a file in the
> initializer __init__.  The processing is fairly substantial, and thus,
> instead of processing the file every time the object is created, I
> pickle the object to a file.
>
> In subsequent creations of the object, I implement a test to see
> whether the pickled file exists.  If it does, then I unpickle the
> object.
>
> Unfortunately, the __init__ cannot return this unpickled object.
>

Try __new__().  http://docs.python.org/ref/customization.html  This isn't
the usual application of __new__, but since it returns an object it should
be ideal for your purposes.









More information about the Python-list mailing list