properly implementing the __getattribute__()

C

Carlo v. Dango

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
 
P

Peter Otten

Carlo said:
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
 
C

Carlo v. Dango

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 said:
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
 
J

John J. Lee

Carlo v. Dango said:
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!
[...]

Only once. Set up your editor(s) properly, and put a check in your
version control system if you have one, or maybe in your test suite,
if it's not too slow (you can use tabnanny from the standard Python
distribution -- it's both a script and a module, IIRC).


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top