__getattr__ related question

Oleg Broytmann phd at phd.pp.ru
Wed Dec 12 08:25:15 EST 2001

On Wed, Dec 12, 2001 at 12:38:28PM -0000, dominikush wrote:
> the __getattr__ method causes me some headache. To prohibit unwanted
> method calls of an object, I shield it by an interface object. So,
> all method calls are addressed to the interface object. That means
> the user calls
>     capsuleInterface.send("Hallo")
> instead of directly calling capsule.send("Hallo"), see example
> below. The __getattr__ in Port catches the call and returns the
> send() method of Capsule. However, evaluating the method fails
>     >>> capsuleInterface.send("Hallo")
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>     TypeError: not enough arguments; expected 2, got 1

   Looks like the wrapper does not pass "self" as a first rgument.

>     def __getattr__(self,name):
>         if name in self.methods:
>             try:
>                 return self.component.__class__.__dict__[name]
>             except KeyError, error:
>                 raise AttributeError, error
>         raise AttributeError, name+" is not known to port"

   Aha, here is the error:
>                 return self.component.__class__.__dict__[name]
   This returns *unbound* method. Return a *bound* method instead:
return getattr(self.component, name)

     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

More information about the Python-list mailing list