Why Does Your Python Function Return None?

Problem scenario
Your Python function prints out the value of the variable immediately before it returns the variable.  When it is assigned via the "return" statement, the value is "None".  You were expecting an integer or some other value.  What is wrong?

Solution
Does your function use recursion?  If it is using recursion rather than call the function again, use the return statement.  For example, this would cause the problem:

def coolfunction(x):
    ....
    coolfunction(x)

This rewrite of the above would solve the problem of the unwanted "None" returning from your function:

def coolfunction(x):
    ....
    return coolfunction(x) 

Leave a comment

Your email address will not be published. Required fields are marked *