Puzzling: local variable in recursive function made global?

Daniel Oberski daniel.oberski at gmail.com
Thu Mar 26 14:55:52 EDT 2009


Hi Peter,

> Plus, it works as expected (read: modifies the argument) if you
> explicitly pass an empty list to the function...

That is not so. The reason is given by Andrew Cooke in this thread.

I would "expect" that when function calls lower in the recursion 
hierarchy return, the object is not changed from the point of view of the 
higher-order calls. This is expectation is mistaken because it is "the 
same frickin object" as Andrew said. One thing that goes wrong for 
example is the last unit test given in the program.

This will happen even if an empty list is explicitly passed in the 
initial call...

Thanks to everyone for responding, this really helped me understand 
better!

Best regards, Daniel



More information about the Python-list mailing list