properly implementing the __getattribute__()

Carlo v. Dango oest at soetu.eu
Tue Oct 7 10:07:58 CEST 2003


On Tue, 07 Oct 2003 09:34:27 +0200, Peter Otten <__peter__ at web.de> wrote:

Many thanks for your quick reply!

With disbelief i watched how your code worked and mine didn't! Then I 
tried running my code with the -tt flag, which revealed a problem with a 
mix of spaces and tabs :-( Solving that made my code work as well! Sigh... 
this sort of problem is something one really has to get used to!

-Carlo



> Carlo v. Dango wrote:
>
>> hello there. I have a hard time implementing __getattribute__ properly. 
>> I
>> know I should stay away from that method, but Im doing some low-level
>> changes to the dispatching and method-lookup, so I really don't have a
>> choice ;-)
>>
>> the following code
>>
>> class Foo(object):
>> def __init__(self):
>> self.name = "hans"
>>
>> def foo(self):
>> print "foo"
>>
>> class Bar(Foo):
>> def info(self):
>> print self.name
>>
>>
>> b = Bar()
>> print dir(b)
>> print b.__dict__
>>
>> results in
>>
>> [...,  'foo', 'info', 'name']
>> {'name': 'hans'}
>>
>>
>>
>> But when I implement a simple __getattribute__
>>
>> class Foo(object):
>>     def __getattribute__(self, name): return 
>> object.__getattribute__(self,
>> name)
>>
>> def __init__(self):
>> self.name = "hans"
>>
>> def foo(self):
>> print "foo"
>>
>> class Bar(Foo):
>> def info(self):
>> print self.name
>>
>> b = Bar()
>> print dir(b)
>> print b.__dict__
>>
>>
>> I get
>>
>> [...,  'info']
>> {}
>>
>>
>> so the method and the field decl of the super type is now gone :( And
>> worse.. if i do a "b.foo()" it fails with the error AttributeError: 
>> 'Bar'
>> object has no attribute 'foo'
>>
>> I'm completely lost
>>
>> Suggestions are deeply appreciated...
>>
>> -Carlo v. Dango
>
> It works as expected here on Linux with both Python 2.2.1 and 2.3
>
> class Foo(object):
>     def __getattribute__(self, name):
>         return object.__getattribute__(self, name)
>
>     def __init__(self):
>         self.name = "hans"
>
>     def foo(self):
>         print "foo"
>
> class Bar(Foo):
>     def info(self):
>         print self.name
>
> def dir2(obj):
>     default = dir(object())
>     return [n for n in dir(obj) if not n in default]
>
> b = Bar()
> print dir2(b)
> # ['__dict__', '__module__', '__weakref__', 'foo', 'info', 'name']
>
> print b.__dict__
> # {'name': 'hans'}
>
> Maybe you are accidentally calling some old version of you test program?
>
> Peter
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/




More information about the Python-list mailing list