Built-In / Library Function Redefined - TypeError: 'x' object is not callable

:point_left: Check out other known bugs

:point_down: Jump to Workaround

Introduction

On your programming journey, there is a high chance that you will come across this weird error message that is ‘int’/‘float’/’something_else’ object is not callable. The reason we get this error message is that we accidentally redefine built-in / user-defined functions by using them as variables. In this post, we will go through some examples of this error and how to debug it.

Note

Another way to get this error is by purposefully calling a variable name as if it’s a function name just to have fun like this :wink: :

string_1 = "I am a string (str) variable. Please don't call me!"
string_1()
Traceback (most recent call last):
  File "script.py", line 2, in <module>
    string_1()
TypeError: 'str' object is not callable

Examples

TypeError: ‘int’ object is not callable

The best way to reproduce this error is to use the below code:

int = 1
float = 3.5
int(float)

Here what happened is that we used int() function as a variable name and assigned 1 to it, which caused the int() function to get redefined. And now when we try to call the int() function to convert a float value, we are getting TypeError: 'int' object is not callable.

TypeError: ‘float’ object is not callable

The best way to reproduce this error is to use a similar case as the previous code:

float = 3.5
int = 1
float(int)

Here what happened is that we used float() function as a variable name and assigned 3.5 to it, which caused the float() function to get redefined. And now when we try to call the float() function to convert an integer value, we are getting TypeError: 'float' object is not callable.

What is the error trying to tell us?

The error is trying to tell us that the function you are trying to call is not a function object anymore, it’s a different object, and that object is not callable.

Workaround

If you are experiencing this error, please click on the :point_up_2: Vote button to help us prioritize this issue. If you need any further help or want to report a new bug, please feel free to Contact Us.

How to debug this error?

Debugging is a process of identifying errors and fixing them. So let’s first identify our error.

How to identify?

float = 3.5
int = 1
float(int)

When you run the above code, you will receive the following Traceback:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    float(int)
TypeError: 'float' object is not callable

If you notice, the Traceback is providing us the line at which this error has occurred. And the error is saying TypeError: 'float' object is not callable. Since we are calling the float() function here, we can see that somehow the ‘function’ object has become a ‘float’ object.

How to fix?

We can look for a variable that shares the same name as the function name and change its name to something else.

However, there are cases where you couldn’t find that variable name. For example, in a Jupyter notebook, if you define a variable, it stays defined until you restart the kernel or delete the variable. So there are chances that you defined a variable named float and removed it from your code. However, since it’s not removed from the global scope, you will keep on getting this error.

A quick way to fix it is by using del float just above the first occurrence of the function name like this:

int = 1
del float
float(int)

This will delete the float variable, and the function definition of float() will get restored. However, when you rerun this code, you will get the following Traceback:

Traceback (most recent call last):
  File "script.py", line 2, in <module>
    del float
NameError: name 'float' is not defined

And that is because on the first run del float has already removed the float variable, so the python interpreter is not able to find a variable named float. To fix this error, remove del float from your code.

Note: If you are getting TypeError: 'x' object is not callable in a library function (for example: plt.xlabel()), then please check out this post:
How do I reload a Python library?

:point_left: Check out other known bugs