Python TypeError: 'NoneType' object is not callable 的解决方案

1,626 阅读4分钟

在Python中,要调用一个函数,我们使用函数名,后面是括号() 。但是如果我们试图调用其他的Python对象,如:int,list,str,tuple, 等等,我们会收到TypeError Exception,错误信息是以下对象不可调用。

None 是Python中的一个保留关键字值,其数据类型是 。如果我们在一个无值后面加上小括号"NoneType()",并试图将其作为一个函数来调用,我们会收到 错误。TypeError: 'NoneType' object is not callable

在这个Python指南中,我们将了解这个Python错误,并讨论为什么它会在Python中出现,以及如何解决它。我们还将讨论一个常见的例子情景,许多Python学习者在编写程序时犯了错误,遇到了这个错误。

那么我们就从错误声明开始吧

问题:TypeError:'NoneType'对象不可调用

TypeError: 'NoneType' object is not callable 是一个标准的Python错误语句,和其他错误语句一样,它可以分为两部分。

  1. 异常类型 TypeError
  2. 错误信息 ( 'NoneType' object is not callable)

类型错误(TypeError)

TypeError 是一个标准的 Python 异常,当我们对一个特定的 Python 对象进行不支持的操作时,它就会在 Python 程序中发生。

例如,整数对象在Python中不支持索引。如果我们试图对一个整数对象执行索引,我们也会收到 TypeError和一些错误信息。

NoneType "对象不可调用

"NoneType 对象不可调用 "是一个错误信息,当我们试图将一个 NoneType 对象作为一个函数来调用时,它会在程序中出现。

在 Python 中只有一个 NoneType 对象值None

# None Value
obj = None

print('Type of obj is: ',type(obj))

输出

Type of obj is: <class 'NoneType'>

如果我们试图用小括号()来调用None值的对象或变量作为一个函数,我们会收到TypeError,信息是'NoneType'对象不可调用'。

例子

# None Value
obj = None

# call None obj as a function 
obj()

输出

Traceback (most recent call last):
  File "main.py", line 5, in 
    obj()
TypeError: 'NoneType' object is not callable

在这个例子中,我们试图将obj 变量作为一个函数来调用obj() ,我们收到了错误。

obj 的值是None ,这使得obj 成为一个NoneType 对象,而我们不能对一个 NoneType 对象进行函数调用。当Python解释器读取语句obj() ,它试图将obj作为一个函数来调用,但是很快它就意识到obj 不是一个函数,解释器抛出了错误。

常见的例子情景

许多Python学习者遇到这个错误的最常见的例子是,他们为一个函数和一个None类型的变量使用相同的名字。

无值主要用于定义初始值,如果我们用无值变量的同名来定义一个函数,我们将在程序中收到这个错误。

例子

让我们创建一个Python函数result() ,接受一个成对的图元列表students [(name,marks)],并返回获得最高分数的学生的姓名和分数。

def result(students):
    # initilize the highest_marks
    highest_marks = 0

    for name, marks in students:
        if marks> highest_marks:
            rank1_name = name
            highest_marks =marks

    return (rank1_name, highest_marks)

students = [("Aditi Musunur", 973),
            ("Amrish Ilyas", 963),
            ("Aprativirya Seshan", 900),
            ("Debasis Sundhararajan", 904),
            ("Dhritiman Salim",978) ]

# initial result value
result = None

# call the result function
result = result(students)

print(f"The Rank 1 Student is {result[0]} with {result[1]} marks")

输出

Traceback (most recent call last):
  File "main.py", line 22, in 
    result = result(students)
TypeError: 'NoneType' object is not callable

破解代码

在这个例子中,我们在22的result = result(students)语句中得到了错误。

这是因为在这一点上,结果不是一个函数名,而是一个None 值变量。就在函数调用语句的上方,我们用result = None 语句将result 值定义为None。

当Python从上到下执行它的代码时,当我们在函数定义之后定义result 的初始值时,result 对象的值从function 变为None 。而Python解释器在解释result() 语句时,将None对象作为一个函数调用。

解决方案

解决上述问题的方法很简单。在编写Python程序时,我们应该始终考虑对函数和值标识符或变量使用不同的名称。

为了解决上面的问题,我们需要做的就是为我们用来存储结果函数的返回值的结果变量定义一个不同的名字。

def result(students):
    # initilize the highest_marks
    highest_marks = 0

    for name, marks in students:
        if marks> highest_marks:
            rank1_name = name
            highest_marks =marks

    return (rank1_name, highest_marks)

students = [("Aditi Musunur", 973),
            ("Amrish Ilyas", 963),
            ("Aprativirya Seshan", 900),
            ("Debasis Sundhararajan", 904),
            ("Dhritiman Salim",978) ]

# initialize the initial value
rank1= None

# call the result function
rank1 = result(students)

print(f"The Rank 1 Student is {rank1[0]} with {rank1[1]} marks")

输出

The Rank 1 Student is Dhritiman Salim with 978 marks

总结

在这个Python错误教程中,我们讨论了TypeError: 'NoneType' object is not callable 错误,并学习了为什么这个错误会在Python中发生以及如何调试它。在你的Python程序中,只有当你试图调用一个None对象作为函数时,你才会遇到这个错误。为了调试这个错误,你需要仔细阅读这个错误,并再次翻阅代码,寻找导致这个错误的那一行。通过一些练习,你将能够在短时间内解决程序中的这个错误。