properly implementing the __getattribute__()

Discussion in 'Python' started by Carlo v. Dango, Oct 6, 2003.

  1. 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


    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Carlo v. Dango, Oct 6, 2003
    #1
    1. Advertising

  2. Carlo v. Dango

    Peter Otten Guest

    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
     
    Peter Otten, Oct 7, 2003
    #2
    1. Advertising

  3. On Tue, 07 Oct 2003 09:34:27 +0200, Peter Otten <> 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/
     
    Carlo v. Dango, Oct 7, 2003
    #3
  4. Carlo v. Dango

    John J. Lee Guest

    "Carlo v. Dango" <> writes:

    > On Tue, 07 Oct 2003 09:34:27 +0200, Peter Otten <> 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!

    [...]

    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
     
    John J. Lee, Oct 8, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruud de Jong
    Replies:
    6
    Views:
    492
    Shalabh Chaturvedi
    Mar 1, 2004
  2. Daniel =?ISO-8859-1?Q?Sch=FCle?=

    __getattribute__

    Daniel =?ISO-8859-1?Q?Sch=FCle?=, Jul 12, 2004, in forum: Python
    Replies:
    1
    Views:
    592
    Peter Hansen
    Jul 12, 2004
  3. Henry 'Pi' James

    Renaming __getattribute__ (PEP?)

    Henry 'Pi' James, Dec 13, 2004, in forum: Python
    Replies:
    2
    Views:
    319
    Nick Coghlan
    Dec 13, 2004
  4. Gigi
    Replies:
    0
    Views:
    475
  5. Sylvain Ferriol

    __getattribute__ for class object

    Sylvain Ferriol, Aug 12, 2005, in forum: Python
    Replies:
    3
    Views:
    302
    Steven Bethard
    Aug 12, 2005
Loading...

Share This Page