Home >Backend Development >Python Tutorial >What Makes Something Callable in Python?
Understanding "Callables" in Python
Python offers a concept known as a "callable," which encompasses anything that can be invoked like a function. The built-in callable function evaluates whether something possesses a __call__ method or a non-zero tp_call member.
The __call__ Method
The __call__ method is invoked when an object is treated like a function. It enables objects to behave like functions, allowing for custom functionality when invoked with parentheses.
Example
Consider the following example:
class Foo: def __call__(self): print('called') foo_instance = Foo() foo_instance() # This invokes the __call__ method
In this case, calling foo_instance() triggers the __call__ method, resulting in "called" being printed to the console.
The above is the detailed content of What Makes Something Callable in Python?. For more information, please follow other related articles on the PHP Chinese website!