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

John Barratt jjb@vivitec.com.au
Thu, 17 Oct 2002 14:56:50 +1000


Dragging up and old topic here... But I am attempting something similar
here with no success.

Basically I want to do something like was demonstrated (using python
2.1, Zope 2.5.1), but to call the 'default' __getattr__ subsequently so
that I can put in my own handlers, before looking for 'standard' attrs
present on this object through the normal __getattr__ method :

__oldgetattr__ = __getattr__
def __getattr__(self, name):
     if name = 'foo':
         return self.foo()

     return self.__oldgetattr__(name)

However just the assigment of __oldgetattr__ gives an attribute error. 
Looking at Andrew Milton's Portable Holes product it seems a similar
attempt to do this is made :

...
  try:
    __oldgetattr__ = ObjectManager.__getattr__
  except:
    try:
      __oldgetattr__ = ObjectManager.inheritedAttribute('__getattr__')
    except:
      def __oldgetattr__(self, name):
        raise AttributeError, name
...

If I try a similar thing to this, I always end up getting the 'old' one
being defined as just the 'raise' statement.  All other attempts to
store the 'old' gettattr results in an attribute error.  Is this simply
not possible with this version of python and hence the current Zope
release? 

Looks like with 2.2 you can do something like this (taken from Guido's
presentation as OSCON 2001) :

class C(object):
    def __getattr__(self, name):
    if name == 'x': return ...
        return object.__getattr__(self, name)

Thanks for any help/pointers!

JB.


Casey Duncan wrote:
> 
> If your __getattr__ fails to find what it wants, it should raise an
> AttributeError. This will give the ball back to the acquisition
> machinery.
> 
> Thusly:
> 
> def __getattr__(self, name):
>     if name = 'foo':
>         return self.foo()
>     raise AttributeError, name
> 
> hth,
> 
> -Casey
> 
> On Tue, 2002-06-04 at 22:28, Erik A. Dahl wrote:
> > Ok I need to override __getattr__ in one of my product classes.  I'm
> > sure this is killing acquisition but not sure about the persistence
> > stuff (I think this is working).  Is there a way to make this work?
> >  Here is what I'm doing:
> >
> > def __getattr__(self, name):
> >     if name == 'myattr':
> >         return self.myattr()
> >
> >
> > I assume that somewhere in the Acquisition code there is a __getattr__
> > but I can't find it.  I tried calling Implicit.__getattr__ but its not
> > there.  If some one has an example that would be great.
> >
> > -EAD
> >
> >
> >
> > _______________________________________________
> > Zope-Dev maillist  -  Zope-Dev@zope.org
> > http://lists.zope.org/mailman/listinfo/zope-dev
> > **  No cross posts or HTML encoding!  **
> > (Related lists -
> >  http://lists.zope.org/mailman/listinfo/zope-announce
> >  http://lists.zope.org/mailman/listinfo/zope )
> 
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope-dev
> **  No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope )