I have some Python code that basically looks like this:
my_start_list = ...
def process ( my_list ):
#do some stuff
if len(my_list) > 1:
process(my_list)
else:
print(my_list)
return my_list
print(process(my_start_list))
The strange thing is: print(my_list)
prints out the correct content. However, the second print statement printing the return value of the function always prints None
.
Even if I replace the normal return
statement with return("abc")
it is still None
.
As the content of the variable seems to be correct one line before the return statement, I don't know where to start debugging. Are there any common problems that may cause this?
Here's what happening:
process(my_start_list)
.if
block is executed if len(my_list) > 1
, and there are no return statement there. Now, since the else
has not been executed and since that is the only place where you have the return clause, you return the default which is None
.To fix this, you'd want to return the list returned by process(my_list)
.
That is:
def process(my_list):
# do some stuff
...
if len(my_list) > 1:
return process(my_list)
else:
print(my_list)
return my_list