__getinitargs__()

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Aug 24 09:07:37 EDT 2000


ddodini at my-deja.com writes:

> Does someone have a sample of making this work in the context of
> persistant objects whose constructor's take arguments?

> class FullyConstructedPersistantObject:
>     def __init__(self, obj_name, obj_id):
>         self.m_name = obj_name
>         self.m_id = obj_id
> 
>     def __getinitargs__(self, shelf_file):
>         shelf = shelve.open(shelf_file)
>         return shelf

How about

class FullyConstructedPersistantObject:
    def __init__(self, obj_name, obj_id):
        self.m_name = obj_name
        self.m_id = obj_id

    def __getinitargs__(self):
        return (self.m_name, self.obj_id)

This object can now be pickled into a shelve. If obj_name and obj_id
are picklable types themselves, you don't need 

Regards,
Martin





More information about the Python-list mailing list