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

Thomas Jollans tjol at tjol.eu
Thu Oct 19 04:22:52 EDT 2017


On 2017-10-19 10:11, ast wrote:
> Surprisingly, __call__ from the class is called, not the
> one defined in the object. Why ?

That's just how dunder methods like __call__ work.

See https://docs.python.org/3/reference/datamodel.html#special-method-names

To quote the docs:

> For instance, if a class defines a method named __getitem__(), and x is an instance of this class, then x[i] is roughly equivalent to type(x).__getitem__(x, i). 

This is also true for __call__. In your case, calling test is
more-or-less equivalent to getattr(type(test), '__call__')(test), NOT
getattr(test, '__call__')().


-- 
Thomas Jollans



More information about the Python-list mailing list