4 Examples to Master Python Callable Function

So, today in this article, we will focus on python callable. We will see what python callable means and how can we use it? If you are entirely unaware of the topic, then by the end of this article, you will learn something new by the end of this article. So, let’s get started.

Python Callable() Function

So, first, see what is callable? Yeah, it is simple. Anything which can be called is known as callable. Now the question arises, how does it relate to the python programming language.

So, if you consider a class, then you can say that whenever there is a need for instantiating a class, we need to call the class following the parenthesis. The need for using the parenthesis is to specify that the given class is called, and we have to instantiate an object for it. So, here, we can say that the given class is callable. Now, this goes true for all the classes, whether it is user-defined or inbuilt.

Now the thing that matters to us is that how can we check whether a class or object or a function is callable or not? To do that python callable() function helps us. This function takes the variable as the argument and returns True/False. Now, before moving to the examples, first, see the syntax of the given function.

Syntax

callable(<variable>)

Example 1:- Callable Classes

# Example of Callable Class

class MyClass:
  a = 5

# Checking weather the class is callable or not
print(callable(MyClass))
my_object = MyClass()    # Calling class to instantiate it's object
print(my_object.a)

Output:

True
5

Similarly, if you consider a function or class method(), then once you define the function, you can call it when needed, and it will do its work. Here, we can also say that functions are callable.

Example 2:- Callable Function

# Example of Callable Function

def my_function():
  print("This is a callable function")

# Checking weather the function is callable or not
print(callable(my_function))
my_function()   # Calling the function

Output:

True
This is a callable function

Python make class Callable

So far, so good. I hope both things are known to you. What is more captivating here is that you can also call the object of the class (if mentioned there explicitly). What does that mean? It means that one needs to mention the block of code that handles what happens when an object is called. If it’s mentioned there, then we can say that object of that class is callable.

Now, the question arises how we can do it? To do that, we need to define the __call__ method in the class whose object we want is to be callable. Let’s take an example to understand it more clearly.

Example 3:- Calling a non-callable object

# Example of a non-callable object

class MyClass:
  a = 5

my_object = MyClass()    # Calling class to instantiate it's object

# Checking whether the object is callable or not
print(callable(my_object))
c = my_object()          # To check the error

Output:

False
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-edd45e6a69a3> in <module>()
      8 # Checking whether the object is callable or not
      9 print(callable(my_object))
---> 10 c = my_object()          # To check whether it is callable or not

TypeError: 'MyClass' object is not callable

Recommended Reading | NumPy.ndarray object is Not Callable: Error and Resolution

Example 4:- Calling a Callable Object

# Example of a callable object

class MyClass:
  def __call__(self):
    print('This function is called under call method')

my_object = MyClass()    # Calling class to instantiate it's object

# Checking whether the object is callable or not
print(callable(my_object))
c = my_object()          

Output:

This function is called under call method

FAQs

Is python callable a built-in function?

Yes, it is in-built in python. The function returns True if the method or class is callable. Otherwise False.

Conclusion

So, today in this article, we learned about python callables. We understood the meaning of callable classes, functions, and objects. We saw how an error is raised when a non-callable object is called, and then we saw how we could make an object callable.

I hope this article has helped you. Thank You

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments