Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NoneType object is not callable (beginner)

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?

like image 367
Seb Avatar asked Mar 19 '12 10:03

Seb


People also ask

How do I fix NoneType error in Python?

The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.

How do you fix a module not callable in Python?

How to fix typeerror: 'module' object is not callable? To fix this error, we need to change the import statement in “mycode.py” file and specify a specific function in our import statement.

Why is my object not callable Python?

The Python "TypeError: 'int' object is not callable" occurs when we try to call an integer value as a function. To solve the error, correct the assignment, make sure not to override the built-in int() function, and resolve any clashes between function and variable names.

What does it mean if module object is not callable?

The "TypeError: 'module' object is not callable" means that we are trying to call a module object instead of a function or a class. To solve the error, we have to access an attribute on the module object that points to the specific function or class.


1 Answers

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> 
like image 155
Tim Pietzcker Avatar answered Sep 19 '22 14:09

Tim Pietzcker