What happens when a __call__ function is defined in both class and object ?

ast nomail at com.invalid
Thu Oct 19 04:11:02 EDT 2017


Hello, please have a look at following code snippet
(python 3.4.4)

class Test:

    a = 1
 
    def __init__(self):
        self.a = 2
        self.f = lambda : print("f from object")
        self.__call__ = lambda : print("__call__ from object")
  
    def __call__(self):
        print("__call__ from class Test")
  
    def f(self):
        print("f from class Test")

test=Test()

>>> test.a
2
ok, a is defined in both the class and the object, it is read from
the object. This is the expected behavior

>>> test.f()
f from object

ok for the same reason

>>> test()
__call__ from class Test


Surprisingly, __call__ from the class is called, not the
one defined in the object. Why ?

Regards



More information about the Python-list mailing list