In programming, a callable is something that can be called. In this post, we want to discuss Python callable() Explained.

In Python, a callable is anything that can be called, using parentheses and maybe with some arguments. Functions, Generators, and Classes are inherently callable in Python. The  <a href="https://www.w3schools.com/python/ref_func_callable.asp">callable()</a> method takes an object and returns a boolean.

  • True – if the object is callable
  • False – if the object is not callable

The callable() method checks if the object is either of the two –

  • An instance of a class with a __call__ method
  • Is of a type that has a which indicates callability such as in functions, classes, etc., or has a non-null tp_call (c struct) member.

Since functions are callable in Python.

Output

This indicates that every time we create a function, Python creates a callable object for it. You can also verify the presence of __call__ attribute.

Output

Similarly for a Class,

Output

However, if you create an object and run callable() method over that then you will observe that class objects are not inherently callable in Python.

Output

If you try to call the object with parentisis you will get an error message saying object is not callble.

Output

However, we can create classes having __call__ method which makes instance of the class callable.

Making Class objects callable in Python

Output
Now if you call the object the __call__ method will run.
Output

Python callable() Explained

Leave a Comment