Understanding how is a function evaluated using recursion
rusi
rustompmody at gmail.com
Thu Sep 26 00:04:04 EDT 2013
On Thursday, September 26, 2013 4:54:22 AM UTC+5:30, Arturo B wrote:
> So I know what recursion is, but I don't know how is
>
> flatten(i)
>
> evaluated, what value does it returns?
When you are a noob, who do you ask? The gurus.
When you are a guru who do you ask? The computer!
And its really quite easy to ask the computer directly. Here's your code with a extra prints
def flatten(l):
print ("Received: %s" % l)
ret = []
for i in l:
if isinstance(i, list) or isinstance(i, tuple):
ret.extend(flatten(i)) #How is flatten(i) evaluated?
else:
ret.append(i)
print ("Returning: %s" % ret)
return ret
Now run it with a couple of different inputs (not neglecting the trivial cases) and see if it does not self-explain.
If still confusing, come back and ask!
More information about the Python-list
mailing list