We will discuss the type error
object is not callable and see how we can fix it. We also discuss utilizing the callable function to check whether the objects are callable or not in Python.
Fix Python Error
object is not callable
When we look at this error(
object is not callable), we probably need to understand what is happening within the Python script. For example, we have a few lines of code to demonstrate the problem and how we fix it.
v=5 g=9 int=3 print(int(g/v*int))
TypeError: 'int' object is not callable
We have three variables
int. We are experiencing this problem because we defined a variable called
int that is a function.
int is a Python built-in function which is why we can not use the
int as a variable name; therefore, when the Python script executes, it is looking to call the variable name
int kind of restricted name within Python, and as a result, it will show you this error when we use it as a variable.
We fixed the error by changing the variable name and running this script again.
v=5 g=9 d=3 print(int(g/v*d))
Remember that when running any program with a function inside your Python script, you should never put that function as a variable name because you will face the same problem. The same problem happens with strings, floats, etc.
If you are getting this error, you first should check whether any of your variables calls a function name or not. We cannot call a variable that stores data type values like in the following code example.
TypeError: 'str' object is not callable
callable() Function in Python
If an object can be called like a function, the
callable() function returns
True and accepts one parameter, which can be any object.
For example, if we have a variable that is
42, this variable refers to an integer number in memory. We can pass
x into the
callable() function and print the result immediately to see whether it returns a
true or a
When executing this, the return value is
false because the integer number
42 is not callable.
Let’s check what happens if the
callable() function calls itself; we type callable inside the
callable() function and print this. We will see the result is
True, and we can also pass the
Suppose we define our custom objects, not built-in objects, where they are callable or not. Let’s create a function called
FUNC() that returns something.
We pass this function name into the
callable() function and print the output, and this is callable. If we call the return value using
FUNC(), that will be an integer and pass it into the
callable() function. The output will return
False, which is not callable because we can not call an integer that we already look at this.
def FUNC(): return 51 print(callable(FUNC)) print(callable(FUNC()))