A callable object in Python is an object that can be called – explicitly initiated to execute. If we attempt to call a not-callable function, we run into this kind TypeError message.
In Python, objects are called by passing arguments in parentheses. For example, a callable object x can be called as x(arg1, arg2, …) or using the method __call__() as x.__call__(arg1, arg2, …). By the way, Python methods starting and ending with double underscores, like __call__(), are special functions called magic or dunder methods.
Knowing if an Object is Callable
One can verify if a Python object is callable using the callable() function. Let’s create some objects in Python and check if they are callable.
1 2 |
# Integer print(callable(8)) |
Output:
False
1 2 |
#String print(callable("may")) |
Output:
False
1 2 |
# Lists print(callable([1,2,4,5])) |
Output:
False
1 2 3 4 |
# Functions def add(): return 3+4 print(callable(add)) |
Output:
True
1 2 3 4 |
#Classes class ExampleClass(): pass print(callable(ExampleClass)) |
Output:
True
1 2 3 |
#Lambdas func = lambda x: x*2 print(callable(func)) |
Output:
True
From these examples, we can see that integers, strings, and lists are not callable, but functions and classes are. In fact, all data types are not callable.
Let’s attempt to call an object that is not callable to reproduce the <object> object is not a callable error.
1 2 |
b_list = [1,4,-3,6] b_list(2) |
Output:
TypeError: 'list' object is not callable
The above mistake often happens when we attempt to access the value of the list at index 2 by using parenthesis (calling the list) instead of using square brackets b_list[2] for list indexing.
TypeError: Class Object is not Callable
In the earlier section, we said class objects are callable. That is a fact, of course, but that is contingent on how you call the class object you are trying to use. This section will discuss how to reproduce this error and how to solve it.
Reproducing Class Object is not a Callable Error
Consider a module and a main script in the same folder. A module in Python is simply a Python file with variables, functions, or classes that can be imported and used in another script – calling it the main script here.
Module: ClassExample.py
1 2 3 4 5 6 7 8 9 |
class MyClass1(): def __init__(self): pass def read_data(self): print("reading data...") # ..some code pass MyClass1 = MyClass1() |
Main script: main_script.py
1 2 3 4 |
import ClassExample s = ClassExample.MyClass1() s.read_data() |
Output:
TypeError: 'MyClass1' object is not callable
Executing the main_script.py file leads to a Class object that is not callable. This error is caused by the module’s line MyClass1 = MyClass1(). In this line, the class MyClass1() is initialized with a variable of the same name. The call of the MyClass1 class again in the main script, which has already been initialized in the module, is like calling MyClass1()(), which leads to the error.
The solution is to initialize the class with a different name in the module script or remove that line altogether and call the class in the main script.
TypeError: Module object is not callable
This error occurs when module importation is not done incorrectly. The example below shows the importation problem caused when a module has the same name as the class being called in the main script.
Module: ClassExample1.py
1 2 3 4 5 6 7 8 |
class ClassExample1: def __init__(self): pass def read_data(self): print("reading data...") # ..some code pass |
Main script: main_script.py
1 2 3 4 |
import ClassExample1 s = ClassExample1() s.read_data() |
Output:
TypeError: 'module' object is not callable
This error occurs because the main script imports the module, not the class. Therefore, the module is called in the second line, not the class defined in the module script.
There are two ways to solve this problem. The first way is to change the importation line as follows
1 2 3 4 |
from ClassExample1 import ClassExample1 s = ClassExample1() s.read_data() |
Output:
reading data…
This means that we want to import a class named ClassExample1 from the module named ClassExample1.
The second way is to edit the call on the main script as follows:
1 2 3 4 |
import ClassExample1 s = ClassExample1.ClassExample1() s.read_data() |
Output:
reading data…
The second line in the main script now means that we import the ClassExample1() class from the ClassExample1 module.
Note: We have the main script and the module in the same folder in the above cases. In the cases where we have modules in a given folder importation line should look like this “from <folder> import <module_name>,” which imports the module itself and not the functions or classes in it.