I tells me line 1 and line 5 (new to debugging/programming, not sure if that helps)
我告诉我第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
TypeError: 'NoneType' object is not callable
Why does it give me that error?
为什么它会给我这个错误?
3 个解决方案
#1
43
You want to pass the function object hi
to your loop()
function, not the result of a call to hi()
(which is None
since hi()
doesn't return anything).
您希望将函数对象hi传递给您的loop()函数,而不是调用hi()的结果(由于hi()不返回任何内容,因此为None)。
So try this:
所以试试这个:
>>> loop(hi, 5)
hi
hi
hi
hi
hi
Perhaps this will help you understand better:
也许这会帮助你更好地理解:
>>> print hi()
hi
None
>>> print hi
<function hi at 0x0000000002422648>
#2
2
Why does it give me that error?
为什么它会给我这个错误?
Because your first parameter you pass to the loop
function is None but your function is expecting an callable object, which None object isn't.
因为传递给循环函数的第一个参数是None,但是你的函数期望一个可调用的对象,而None对象则不是。
Therefore you have to pass the callable-object which is in your case the hi
function object.
因此,您必须传递可调用对象,在您的情况下是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)
#3
2
You should not pass the call function hi() to the loop() function, This will give the result.
你不应该将调用函数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
#1
43
You want to pass the function object hi
to your loop()
function, not the result of a call to hi()
(which is None
since hi()
doesn't return anything).
您希望将函数对象hi传递给您的loop()函数,而不是调用hi()的结果(由于hi()不返回任何内容,因此为None)。
So try this:
所以试试这个:
>>> loop(hi, 5)
hi
hi
hi
hi
hi
Perhaps this will help you understand better:
也许这会帮助你更好地理解:
>>> print hi()
hi
None
>>> print hi
<function hi at 0x0000000002422648>
#2
2
Why does it give me that error?
为什么它会给我这个错误?
Because your first parameter you pass to the loop
function is None but your function is expecting an callable object, which None object isn't.
因为传递给循环函数的第一个参数是None,但是你的函数期望一个可调用的对象,而None对象则不是。
Therefore you have to pass the callable-object which is in your case the hi
function object.
因此,您必须传递可调用对象,在您的情况下是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)
#3
2
You should not pass the call function hi() to the loop() function, This will give the result.
你不应该将调用函数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