O'Reilly logo

Python Unlocked by Arun Tigeraniya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making calls to objects

Key 4: All objects can be made callable.

To reuse and group code for some task, we group it in the functions classes, and then call it with different inputs. The objects that have a __call__ attribute are callable and __call__ is the entry point. For the C class, tp_call is checked in its structure:

>>> def func(): # a function ... print("adf") ... >>> func() adf >>> func.__call__() #implicit call method adf >>> func.__class__.__call__(func) adf >>> func.__call__ <method-wrapper '__call__' of function object at 0x7ff7d9f24ea0> >>> class C: #a callable class ... def __call__(self): ... print("adf") ... >>> c = C() >>> c() adf >>> c.__call__() #implicit passing of self adf >>> c.__class__.__call__(c) #explicit passing of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required