Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python callable() Explained

2 min read

In programming, a callable is something that can be called.

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 callable() 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.

def my_function():
    print("Hi, I'm a function")

callable(my_function)

Output

True

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.

my_function.__call__

Output

<method-wrapper '__call__' of function object at 0x7f08706d5840>

Similarly for a Class,

class MyClass():
    def my_method(self):
        print("Hi, I am a class method")

callable(MyClass)

Output

True

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.

my_obj = MyClass()
callable(my_obj)

Output

False

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

my_obj()

Output

TypeError: 'MyClass' object is not callable

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

Making Class objects callable in Python

class MyClass():
    def __call__(self):
        print("I am callable now")

    def my_method(self):
        print("Hi, I am a class method")

my_obj = MyClass()
callable(my_obj)

Output

True

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

my_obj()

Output

I am callable now


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read