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 {}. My guess is that this is because __dict__ access is not persistence-safe (perhaps the {} bug occurs when the object is in it's "phantom" state?). Anyway, in that case, I could work around the problem by listing the attributes I wanted explicitly (actually I used the names defined in the object's Interface, which is kind of cool from a design perspective, but that's another issue). Anyway, is there a safer way to get the same information as __dict__ from a Persistent object? Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
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@zope.com V.P. Engineering 540.361.1716 Zope Corporation http://www.zope.com
participants (2)
-
Brian Lloyd -
Terry Hancock