Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is __getattr__ capable of handling built-in operator overloads in Python 2.x but not in Python 3.x?

Tags:

python

class

In python 2.x take the following class:

class Person:
    def __init__(self, name):
        self.name = name

    def myrepr(self):
        return str(self.name)

    def __getattr__(self, attr):
        print('Fetching attr: %s' % attr)
        if attr=='__repr__':
            return self.myrepr

Now if you create an instance and echo it in the shell (to call __repr__), like

 p = Person('Bob')
 p

You get

Fetching attr: __repr__
Bob

Without the __getattr__ overload you'd have just got the default <__main__.A instance at 0x7fb8800c6e18> kind of thing.

My question is why is the built-in __getattr__ even capable of handling calls to __repr__ (and other builtins like that) if they are not elsewhere defined. These are not attributes they are operators, more like methods..

(This no longer works in python 3.x so I guess they got rid of the handling by __getattr__ of the builtins.)

like image 458
fpghost Avatar asked Feb 11 '23 12:02

fpghost


1 Answers

It's not a Python 2 or 3 thing, it's a new-style vs old-style class thing. In old-style classes these methods had no special meaning, they were treated like simple attributes.

In new-style classes the special methods are always looked up in the class(implicit lookup) not instance.

For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

Old-style classes:

For old-style classes, special methods are always looked up in exactly the same way as any other method or attribute. This is the case regardless of whether the method is being looked up explicitly as in x.__getitem__(i) or implicitly as in x[i].

Related: Overriding special methods on an instance

like image 112
Ashwini Chaudhary Avatar answered Feb 14 '23 01:02

Ashwini Chaudhary