On Friday 24 August 2001 13:53, Tres Seaver wrote:
_attributes = PersistentMapping()
Note that this spelling make '_attributes' a "shared instance attribute" (all instances of the class, or ony class derived from it, will share the mapping.
Oops - that certainly wasn't intentionally... I'm still very much of a Python newbie. Must have mixed it up with something else. I thought it was the notation for a pseudo private attribute. Need to pickup "Programming Python" a bit more often :-)
Normally, I would expect to see the '_attibutes' assigned either in the initializer::
def __init__( self ):
self._attributes = PersistentMapping()
or else "lazily"::
_attributes = None;
def __getattr__( self, name ):
if self._attributes is None: self._attributes = PersistentMapping()
if self._attributes.has_key( name ): #...
The lazy approach looks very sensible.
It works great - until the object gets unloaded from the memory :-(
What am I doing wrong?
Note as well that plaing with '__getattr__' in the presence of the acquisition machinery could be classifieds as Deep Voodoo (tm);
:-) Are there any serious pitfalls I ought to know about?
The canonical solution to your problem is to set a "shared" value
for the new attribute on the class, with an appropriate default;
instances which don't have the attribute in their own '__dict__' will find it in the class. Where that solution is infeasible, the pickling machinery offers the '__setstate__' hook, which is called immediately on unpickling; you would *really* prefer not to use this hook, but it is available.
Been there - done that ;-) But not really successfully.
Tres.
-- Regards, Thomas Olsen http://www.tanghus.dk