[Zope-dev] _getOb or __getattr__?

Shane Hathaway shane@digicool.com
Tue, 27 Feb 2001 17:40:44 -0500


Chris Withers wrote:
> 
> Hi,
> 
> I need a product with custom attribute getting code and so looked to the
> BTreeFolder product for inspiration.
> 
> It implements both __getattr__ and _getOb which appear to do roughly the same
> thing.
> 
> What's the difference?

_getOb() is part of the ObjectManager interface.  If you want to store
objects in your special ObjectManager in a special way, you override
_getOb(), _setOb(), and _delOb().  (I didn't check the spelling--see
ObjectManager.py)

However, if you store objects in your ObjectManager in a different way
than using setattr(self, name, value), acquisition won't find your
subobjects because acquisition only looks at object attributes.  It
doesn't know anything about _getOb().  (And it's not a good idea to
teach it to use _getOb(); think what it would be like if a Xeon ran like
a 386...)

Overriding __getattr__() lets you make acquisition work again.  However,
as Jeffrey said, until a few months ago it was forbidden.  Then Jim
fixed it.  Now it's used extensively by BTreeFolders, Transparent
Folders, and even the CMF.  However, every time the acquisition
machinery calls a __getattr__() you get a performance hit in the range
of a few microseconds.  But the products I listed try hard to minimize
that impact, so it's not enough of an issue to warrant a C
implementation.

BTreeFolder.py provides a base class that doesn't have __getattr__(). 
You can use that if you think __getattr__() is slowing down your site. 
The derived class is what most people use because it provides the full
functionality.

Shane