Python NoneType object is not callable (beginner)

Seb picture Seb · Mar 19, 2012 · Viewed 247.5k times · Source

It tells me line 1 and line 5 (new to debugging/programming, not sure if that helps)

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

Why does it give me this error?

Answer

Tim Pietzcker picture Tim Pietzcker · Mar 19, 2012

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).

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>