namespace curiosity

J. Michael Caine michael at doubleserver.com
Sat Nov 25 01:33:47 CET 2006


I just noticed something that may well have an obvious explanation (apologies 
in advance if so).

Little demo illustrates:

>>> def x(y = {1:1}):
...     y[1] = y[1] + 1
...     print y[1]
...
>>> x()
2
>>> x()
3

and so on; y continues to be updated rather than reset each call to x().  
dir() suggests that y did _not_ somehow get into the global namespace, nor do 
I see anything else that explains the behavior obviously.  This only occurs 
with dicts (or rather, possibly with any object that is merely updated within 
x(), rather than reassigned within x()... I haven't tested).

I've been pythoning for a little while now, but haven't participated in this 
usenet; you're welcome to consider me a newbie and be gentle with any 
explanation of my oversight or whatever.  The nature of this problem made it 
difficult for me to investigate - to see if anyone else has reported or 
explained it.

TIA.



More information about the Python-list mailing list