[Zope-dev] How to override __getattr__ and not break acquisition

Casey Duncan casey@zope.com
Thu, 17 Oct 2002 09:24:23 -0400


On Thu, 17 Oct 2002 19:57:40 +1000
John Barratt <jjb@vivitec.com.au> wrote:
[snip]
> 
> I perhaps should have clarified that I need to be able to specify the
> name of the attributes or methods at run time, so ComputedAttribute
> unfortunately won't do the trick as you have to define each
> attribute/method you require in the class definition.
> 
> ie. I need something like :
> def __getattr__(self,attr):
>    if name in self.methodlist:
>      <do something>
>    return Implicit.__class__.__getattr__(self,attr)
> 
> where self.methodlist is a list of strings that may change per instance
> and/or at runtime, or itself could be calculated at the time of doing
> the __getattr__ (eg self.getMethodList()).

__getattr__ hooks are evil, only to be used as a last resort. Are you creating some sort of transparent proxy object? What exactly are you trying to do? I'm not getting a big picture here. And why do you need to call the inherited __getattr__ from Implicit? Doesn't raising an AttributeError take care of this?
 
> One other way that may work is to be able to set methods on the object
> as it comes out of the ZODB for use, as I need to be able to get methods
> from other objects in the ZODB, something to explore... Anyone have any
> pointers on this one perhaps?

Yes, but that would change the attributes and cause the ZODB to save them on commit (unless you use volatile '_v_' attrs). This might not be a bad thing.

-Casey