[Zope] Persistence-safe __dict__?

Brian Lloyd brian at zope.com
Tue Jan 6 12:38:56 EST 2004


> Every once in awhile, it's handy in Python to just access 
> the __dict__ attribute and get the whole list of defined
> names in a class or instance.  It's not always well-advised,
> but sometimes it's awfully useful.
> 
> I unwittingly used this technique with a Zope persistent
> object recently, and got some extremely weird errors due
> to __dict__ occasionally evaluating as {}.

You are correct that getting __dict__ directly could get you 
an empty dict if the object is a 'ghost' (state not yet loaded).

State is generally loaded on demand on persistence boundaries, 
so a cheap hack would be to do: getattr(ob, 'foo', None) on 
an object before you look in the dict. That should force the 
state to be loaded (note that 'foo' need not be a valid attr 
of the object -- the getattr attempt on any name should force 
the state to be loaded).

hope this helps,

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com 





More information about the Zope mailing list