Python

Python Object is Not Callable Error

The object not callable error is common in Python and is caused when the user tries to create a variable by naming it equal to a reserved keyword, thus causing the program to fail to differentiate between the variable name and the reserved keyword. This post will act as a guide and explain the reasons and the solution to the error “Object not callable” in Python.

Reason 1: Invalid naming of Variable in Python

As mentioned above, this error can be caused when the user names a variable equal to a reserved keyword and uses that reserved keyword inside the same program as well. An example to demonstrate this error can be recreated using the following code snippet:

int = 5
var1 = int(7)
for i in range(1, var1):
  print(i * 5)

When you execute this code, it will produce:

In this example, the error is caused because the program is trying to call the user-created variable “int” as a function, which is not possible.

Solution: Provide Proper Identifiers to Variables

To fix this issue, the user can provide a different name to the variable at the first line, and the correct code for this example is:

a = 5
var1 = int(7)
for i in range(1, var1):
  print(i * 5)

When this code is executed now, it will produce the following result on the terminal:

As you can see that the program has successfully executed without any errors.

Reason 2: Incorrect Call to Imported Module’s Method

This error also occurs when the user is not correctly using the module import while accessing its methods; to demonstrate this, take the following code snippet:

import socket

var1 = socket(socket.AF_INET, socket.SOCK_STREAM)

print("The output of the Socket Variable is as: ")
print(var1)

In this code snippet:

  • The user has imported the module “socket” into the code
  • This module has a method named as socket(), and the user is making a call to it without mentioning the module name with a dot operator

When this code is executed, it produces the following result:

Let’s see how to fix this error.

Solution 1: Use Module Name With Dot Operator

Well, this solution is pretty straightforward; when calling the method, use the module name and access its method after applying the dot operator, like so:

import socket

var1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("The output of the Socket Variable is as: ")
print(var1)

This time when the code is executed, it produces the following outcome on the terminal:

As you can see, the program was able to execute without any errors.

Solution 2: Using from-import Approach

If you do not wish to use the module’s name again throughout the program, you can change the way you are importing the module in your program; and instead of using the “import module,” you can use the “from module import *”. What this does is that it directly adds the methods of the module into your program.

With this solution, the correct code is:

from socket import *

var1 = socket(AF_INET, SOCK_STREAM)

print("The output of the Socket Variable is as: ")
print(var1)

This time around when this code is executed, it will produce the following outcome:

The output confirms that the error was successfully avoided.

Conclusion

The Python error “Object is not callable” is often caused by calling a variable as a function/module when that variable is only storing a simple value instead of being a method. Moreover, when the variable name is set as a reserved keyword for the built-in method, and the built-in method is also used within the same program, it also causes this error. One last common reason for this error is the incorrect way of importing a module and calling its method, which has been explained in this guide.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.