[英]Python NoneType object is not callable (beginner)
它告诉我第 1 行和第 5 行(调试/编程新手,不确定是否有帮助)
def hi():
print('hi')
def loop(f, n): # f repeats n times
if n <= 0:
return
else:
f()
loop(f, n-1)
>>> loop(hi(), 5)
hi
f()
TypeError: 'NoneType' object is not callable
为什么它给我这个错误?
您想将函数对象hi
传递给您的loop()
函数,而不是调用hi()
(由于hi()
不返回任何内容,因此为None
)。
所以试试这个:
>>> loop(hi, 5)
hi
hi
hi
hi
hi
也许这会帮助你更好地理解:
>>> print hi()
hi
None
>>> print hi
<function hi at 0x0000000002422648>
您不应该将调用函数 hi() 传递给 loop() 函数,这将给出结果。
def hi():
print('hi')
def loop(f, n): #f repeats n times
if n<=0:
return
else:
f()
loop(f, n-1)
loop(hi, 5) # Do not use hi() function inside loop() function
我遇到了错误“TypeError: 'NoneType' object is not callable”,但针对另一个问题。 有了上面的线索,我就能够调试并做对了! 我面临的问题是:我编写了客户库,尽管我已经提到过,但我的文件无法识别它
example:
Library ../../../libraries/customlibraries/ExtendedWaitKeywords.py
the keywords from my custom library were recognized and that error was resolved only after specifying the complete path, as it was not getting the callable function.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.