TypeError: ‘int’ object is not callable in Python [已解决]

TypeError: ‘int’ 对象在 Python 中不可调用

TypeError: ‘int’ object is not callable in Python

当我们尝试将整数值作为函数调用时,会出现 Python“TypeError: ‘int’ object is not callable”。

要解决错误,请更正分配,确保不要覆盖内置int()函数并解决函数名和变量名之间的任何冲突。

typeerror int 对象不可调用

下面是错误如何发生的示例。

主程序
example = 100 # ⛔️ TypeError: 'int' object is not callable example()
您必须追踪变量在何处被分配了整数值,并更正分配给函数或类的值,或者删除括号。

覆盖内置int()函数

确保您没有覆盖内置int()函数。

主程序
# 👇️ overrides built-in int() function int = 150 # ⛔️ TypeError: 'int' object is not callable print(int('150'))

我们覆盖内置int函数,将其设置为整数值,并尝试将该整数作为函数调用。

您可以通过重命名变量来解决错误。

主程序
# ✅ Not overriding built-in int() anymore my_int = 150 print(int('150')) # 👉️ 150

该变量不再与内置int()函数冲突,因此问题已解决。

确保您没有隐藏内置函数的变量,例如
int, sum, min,max等。

缺少数学运算符

当您缺少数学运算符时,也会发生该错误。

主程序
# ⛔️ TypeError: 'int' object is not callable result = 2 (5 * 5)

2在和括号组之间没有操作,因此 Python 解释器假定我们正在尝试调用整数2

To solve the error, specify the missing operator.

main.py
# ✅ multiplication result = 2 * (5 * 5) print(result) # 👉️ 50 # ✅ division result = 2 / (5 * 5) print(result) # 👉️ 0.08 # ✅ subtraction result = 2 - (5 * 5) print(result) # 👉️ -23 # ✅ addition result = 2 + (5 * 5) print(result) # 👉️ 27

Now the interpreter knows that we aren’t trying to invoke the integer value 2.

Having a function and a variable with the same name #

Another common cause of the error is having a function and a variable that share
the same name.

main.py
def example(): return 'bobbyhadz.com' example = 100 # ⛔️ TypeError: 'int' object is not callable example()

The example variable shadows the function with the same name, so when we try
to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

main.py
def example(): return 'bobbyhadz.com' my_int = 100 print(example()) # 👉️ "bobbyhadz.com"

We gave the variable a different name, so it no longer clashes with the
function.

Common causes of the error #

The error occurs for multiple reasons:

  • Trying to call an integer value with parentheses ().
  • Having a missing mathematical operator.
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to an integer.
  • Having a class method and a class property with the same name.
  • Calling a function that returns an integer value twice.

Having a class method and property with the same name #

The error is also caused when we have a class method and a class property with
the same name.

main.py
class Employee: def __init__(self, salary): self.salary = salary # 👇️ same name as class variable def salary(self): return self.salary emp = Employee(100) # ⛔️ TypeError: 'int' object is not callable emp.salary()

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee: def __init__(self, salary): self.salary = salary def get_salary(self): return self.salary emp = Employee(100) print(emp.get_salary()) # 👉️ 100

Once you rename the method, you will be able to call it without any issues.

Trying to call a function that returns an integer twice #

Make sure you aren’t trying to call a function that returns an integer twice.

main.py
def get_num(): return 100 # ⛔️ TypeError: 'int' object is not callable get_num()() # 👈️ remove second set of parentheses

Notice that we used two sets of parentheses when calling the get_num function.

The first set of parentheses calls the function and the function returns an
integer.

The second set of parentheses tries to call the integer and causes the error.

To solve the error, remove the second set of parentheses.

main.py
def get_num(): return 100 print(get_num()) # 👉️ 100

Conclusion #

To solve the “TypeError: ‘int’ object is not callable” error, make sure:

  • You aren’t trying to call an integer with parentheses.
  • You don’t have a missing mathematical operator.
  • You don’t have a function and a variable with the same name.
  • You aren’t overriding a built-in function and setting it to a boolean.
  • You don’t have a class method and a property with the same name.
  • 您不是在调用一个返回整数两次的函数。