Python 错误 TypeError 解析,实际错误实例详解 (一)

时间:2025-04-07 09:33:53

文章目录

    • 前言
    • Python 错误 TypeError: Int object Is Not Callable
      • python 中添加缺少的运算符以修复 TypeError: 'int' object is not callable in Python
      • Python 中更改变量名称以修复 TypeError: 'int' object is not callable
    • Python 错误 TypeError: Unhashable Type: List
      • Python 中的 TypeError: unhashable type: 'list'
      • Python 中的哈希函数
      • 修复 Python 中的 TypeError: unhashable type: 'list'
    • Python 错误 TypeError: Unsupported Operand Type(s) for +: ‘NoneType‘ and ‘Int‘
      • Python 中错误 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 原因
      • 修复 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
    • 总结

前言

本篇文章我们通过几个实际的实例来看一下 TypeError 错误,并给出解决方案。大家在以后的编码中可以举一反三,从而有效的避免此类TypeError 错误。本篇内容需要你收藏起来慢慢看,也可以作为一个字典,碰到相关的可以直接参考相应的解决方法。

OK, Let’s go.


Python 错误 TypeError: Int object Is Not Callable

这是使用 Python 编码时遇到的常见错误之一。 Python 可能在多种情况下引发此错误。

主要是当您在执行计算时错过数学运算符并在代码中使用内置函数作为变量时,就会发生这种情况。

本节内容将讨论所有场景以及修复 TypeError: 'int' object is not callable 的解决方案。

python 中添加缺少的运算符以修复 TypeError: ‘int’ object is not callable in Python

有时您可能忘记在代码中添加数学运算符。 结果,你会得到一个 TypeError: ‘int’ object is not callable。

让我们以这个简单的 Python 脚本为例。

marks_obtained=450
total_marks=600
percentage=100(marks_obtained/total_marks)
print("The percentage is:", percentage)
  • 1
  • 2
  • 3
  • 4

输出:

Traceback (most recent call last):
  File "c:\Users\rhntm\", line 3, in <module>
    percentage=100(marks_obtained/total_marks)
TypeError: 'int' object is not callable
  • 1
  • 2
  • 3
  • 4

它返回错误,因为百分比计算代码中缺少乘法运算符。 您可以通过在代码中添加乘法运算符 * 来解决此问题。

marks_obtained=450
total_marks=600
percentage=100*(marks_obtained/total_marks)
print("The percentage is:", percentage)
  • 1
  • 2
  • 3
  • 4

输出:

The percentage is: 75.0
  • 1

Python 中更改变量名称以修复 TypeError: ‘int’ object is not callable

如果您使用变量的内置函数名称并稍后调用该函数,您将收到一条错误消息 ‘int’ object is not callable

在下面的示例中,我们声明了一个 sum 变量,sum() 是 Python 中的内置函数,用于添加迭代器的项。

num=[2,4,6,8]
sum=0
sum=sum(num)
print("The sum is:", sum)
  • 1
  • 2
  • 3
  • 4

输出:

Traceback (most recent call last):
  File "c:\Users\rhntm\", line 3, in <module>
    sum=sum(num)
TypeError: 'int' object is not callable
  • 1
  • 2
  • 3
  • 4

由于我们使用了 sum 变量,并随后调用了 sum() 函数来计算列表中数字的总和,因此它会引发 TypeError,因为 sum 变量覆盖了 sum() 方法。

您可以通过重命名变量名称来修复此类错误。 在这里,我们将变量 sum 更改为 Total。

num=[2,4,6,8]
total=0
total=sum(num)
print("The sum is:", total)
  • 1
  • 2
  • 3
  • 4

输出:

The sum is: 20
  • 1

可以看到,这次代码运行成功了。

现在您知道如何修复 Python 中的 ‘int’ object is not callable 错误。 我们希望这些解决方案对您有所帮助。


Python 错误 TypeError: Unhashable Type: List

本节内容将讨论 TypeError: unhashable type: 'list' 以及如何在 Python 中修复它。

Python 中的 TypeError: unhashable type: ‘list’

我们先来看该错误是如何产生的。

当您将不可散列的对象(如列表)作为键传递给 Python 字典或查找函数的散列值时,会发生此错误。

字典是 Python 中的一种数据结构,以键值对的形式工作,每个键都有一个对应的值,要访问值的值,您将需要像数组索引这样的键。

字典的语法:

dic ={ "key": "Values"}
  • 1

可哈希对象是那些值不随时间变化但保持不变的对象,元组和字符串是可哈希对象的类型。

代码:

# creating a dictionary
dic = {
    # list as a key --> Error because lists are immutable
    ["a","b"] : [1,2]
}

print(dic)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输出:

TypeError: unhashable type: 'list'
  • 1

我们使用列表 ["a","b"] 作为键,但编译器抛出了一个 TypeError: unhashable type: ‘list’

让我们手动查找列表的哈希值。

代码:

lst = ["a","b"]

hash_value = hash(lst)

print(hash_value)
  • 1
  • 2
  • 3
  • 4
  • 5

输出:

TypeError: unhashable type: 'list'
  • 1

hash() 函数用于查找给定对象的哈希值,但该对象必须是不可变的,如字符串、元组等。

Python 中的哈希函数

hash() 函数是一种加密技术,它加密不可变对象并为其分配一个唯一值,称为对象的哈希值。 无论数据大小如何,它都提供相同大小的唯一值。

代码:

string_val = "String Value"
tuple_val = (1,2,3,4,5)
msg= """Hey there!
              Welcome to """

print("Hash of a string object\t\t", hash(string_val))
print("Hash of a tuple object\t\t", hash(tuple_val))
print("Hash of a string message\t", hash(tuple_val))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出:

Hash of a string object      -74188595
Hash of a tuple object       -1883319094
Hash of a string message     -1883319094
  • 1
  • 2
  • 3

散列值大小相同,并且对于每个值都是唯一的。

修复 Python 中的 TypeError: unhashable type: ‘list’

要修复 Python 中的 TypeError,您必须使用不可变对象作为字典的键和 hash() 函数的参数。 请注意,在上面的代码中,hash() 函数与元组和字符串等可变对象完美配合。

让我们看看如何修复字典中的 TypeError: unhashable type: ‘list’

代码:

# creating a dictionary
dic = {
    # string as key
    "a" : [1,2]
}

print(dic)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输出:

{'a': [1, 2]}
  • 1

这次我们提供一个字符串“a”作为键,使用它很好,因为字符串是可变的。


Python 错误 TypeError: Unsupported Operand Type(s) for +: ‘NoneType‘ and ‘Int‘

在 Python 中,当您将整数值与空值相加时,会出现 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 。 我们将在本文中讨论 Python 错误以及如何解决它。

Python 中错误 TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ 原因

Python 编译器会抛出 TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ 因为我们正在操作具有不同数据类型的两个值。 在本例中,这些值的数据类型为 int 和 null,并且该错误会告诉您不支持该操作,因为 + 运算符的操作数 int 和 null 无效。

代码示例:

a = None
b = 32

print("The data type of a is ", type(a))
print("The data type of b is ", type(b))

# TypeError --> unsupported operand type(s) for +: 'NoneType' and 'int'
result = a+b
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出:

The data type of a is  <class 'NoneType'>
The data type of b is  <class 'int'>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
  • 1
  • 2
  • 3

正如我们在上面程序的输出中看到的,a 的数据类型是 NoneType,而 b 的数据类型是 int。 当我们尝试添加变量 a 和 b 时,我们遇到 TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’

这里有一些类似的情况,也会导致 TypeError,因为它们的数据类型不同。

# Adding a string and an integer
a = "string"
b = 32
a+b         # --> Error

# Adding a Null value and a string
a = None
b = "string"
a+b         # --> Error

# Adding char value and a float
a = 'D'
b = 1.1
a+b         # --> Error
Fix the TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' in Python
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

不能使用 null 对其进行算术运算,并且上面的程序已证明它会引发错误。 此外,如果您有任何类似的情况,请在执行任何算术运算或其他所需任务之前对值进行类型转换。

要修复此错误,您可以使用有效的数据类型对其执行任何算术运算,对值进行类型转换,或者如果您的函数返回空值,您可以使用 try-catch 块来避免程序崩溃。

代码示例:

a = "fql"
b = "jiyik"

print("The data type of a is ", type(a))
print("The data type of b is ", type(b))

result = a+b

print(result)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

输出:

The data type of a is  <class 'str'>
The data type of b is  <class 'str'>
fqljiyik
  • 1
  • 2
  • 3

正如您所看到的,a 和 b 具有相似的数据类型,它们完美连接而不会引发任何错误,因为这两个变量的性质是相同的。

修复 TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’

代码示例:

def sum_ab(a, b=None):
        return a+b #TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

sum_ab(3)
  • 1
  • 2
  • 3
  • 4

输出:

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
  • 1

在上面的代码中,sum_ab() 函数有两个参数,a 和 b,而 b 被分配给空值(默认参数),并且该函数返回 a 和 b 的和。

假设您只提供了一个参数 sum_ab(3)。 该函数会自动触发默认参数为None,无法添加,如上面的示例所示。

在这种情况下,如果您不确定哪个函数引发了 TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘int’ ,您可以使用 try-catch 机制来克服此类错误。

代码示例:

try:
    def sum_ab(a, b=None):
        return a+b

    sum_ab(3)

except TypeError:
    print(" unsupported operand type(s) for +: 'int' and 'NoneType' \n The data types are a and b are invalid")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出:

 unsupported operand type(s) for +: 'int' and 'NoneType'
 The data types are a and b are invalid
  • 1
  • 2

try-catch 块可以帮助您解释错误并防止程序崩溃。


总结

上面我们通过三个实例来介绍了TypeError的情况。先是如何产生的,然后给出原因以及解决的方案。由于篇幅原因,我们在本篇中就先给出这三个实例。在下一篇文章中我们继续。下篇内容我们再见。