multiple inheritance and __getattr__

Enrico 4564 at 755189.45
Tue Jul 29 09:45:07 CEST 2008


"Bruno Desthuilliers" <bdesth.quelquechose at free.quelquepart.fr> ha scritto
nel messaggio news:488e2e70$0$29472$426a74cc at news.free.fr...
> Indeed. You explicitely raise, so the lookup stops here. You'd need to
> explicitely call on superclass instead to have B.__getattr__ called, ie:
>
> class A(object):
>      def __getattr__(self, name):
>          if name == 'a':
>              return 1
>          return super(A, self).__getattr__(name)
>
> class B(object):
>      def __getattr__(self, name):
>          if name == 'b':
>              return 2
>          return super(B, self).__getattr__(name)

Hi Bruno,
this is an interisting point. Just to understand better: when I raise an
AttributeError the search stops but if I call a superclass (that for my
understanding raises an AttributeError) the search continues. At this point
I suspect that the search is doing something else, like checking if the
class is at the top of the hierarchy. Do you know where I can look for this,
probably in the core code of Python?

> > Since A and B are not written by me I can only work on C.
>
> Really ? You know, Python is a *very* dynamic language. If A and B are
> ordinary Python classes (ie: not builtin types, not C extensions, etc),
> you can monkeypatch them. But that's not necessarily the best thing to
> do (it would require more work than your actual solution).

I know that I can do whatIwant with class A and class B (monkeypatch!) but I
prefer to concentrate on my code and write a clean solution. Thanks for your
help.
Enrico





More information about the Python-list mailing list