Python Class Object Is Not Callable

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.

Output:

False

Output:

False

Output:

False

Output:

True

Output:

True

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.

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

Main script: main_script.py

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

Main script: main_script.py

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

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:

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.