Puzzling: local variable in recursive function made global?

Daniel Oberski daniel.oberski at gmail.com
Thu Mar 26 14:45:59 EDT 2009


Hi Andrew,


> it's not global, it's mutable.  you are passing THE SAME FRICKING OBJECT
> to is_terminal and appending values to it.

That, I understand. I already saw in the archives this confuses many 
people, e.g. the thread "Odd behavior regarding a list". I think this is 
understandable if you did not read the documentation's small print about 
mutable objects. However, that was not my problem in this case.

What I was surprised about is that this object persists across calls to 
is_terminal(). Diez now showed me why that is: the def statement creates 
a persistent object upon the first time it is called.

> sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE
> ANY MORE.

No problem, thanks for answering anyway.

all the best,

daniel



More information about the Python-list mailing list