super, decorators and gettattribute

Rhamphoryncus rhamph at gmail.com
Tue Jan 15 18:23:53 CET 2008


On Jan 13, 5:51 am, Richard Szopa <ryszard.sz... at gmail.com> wrote:
> On Jan 13, 8:59 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>
> > On Sat, 12 Jan 2008 14:23:52 -0800, Richard Szopa wrote:
> > > However, I am very surprised to learn that
>
> > > super_object.__getattr__(name)(*args, **kwargs)
>
> > > getattr(super_object, name)(*args, **kwargs)
>
> > > are not equivalent. This is quite odd, at least when with len()
> > > and .__len__, str() and .__str__. Do you maybe know what's the
> > > rationale behind not following that convention by getattr?
>
> > I think you are confusing `__getattr__` and `__getattribute__` here!
> > `getattr()` maps to `__getattr__()`, it's `__getattribute__` that's
> > different.
>
> Well, in my code calling super_object.__getattr__(name)(*args,
> **kwargs) and getattr(super_object, name)(*args, **kwargs) gives
> *different* effects (namely, the latter works, while the former
> doesn't). That kinda suggests that they don't map to each other :-).
> And that makes me feel confused.

Don't think of them as mappings.  Think of them as a way for a class
to hook into getattr's protocol, conveniently named similar to getattr
(__getattr__ and __getattribute__).  getattr() may call several
methods, no methods at all, change the arguments, etc.  Although len()
may seem simple, many others are not so simple.

--
Adam Olsen, aka Rhamphoryncus



More information about the Python-list mailing list