繁体   English   中英

Python NoneType 对象不可调用(初学者)

[英]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>

为什么它给我那个错误?

因为您传递给loop函数的第一个参数是None但您的函数需要一个可调用对象,而None对象不是。

因此,您必须传递在您的情况下是hi函数对象的可调用对象。

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() 传递给 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM