[Tutor] dictionaries, objects and scoping...

Kent Johnson kent37 at tds.net
Tue Jan 22 03:35:14 CET 2008


John Morris wrote:
> Does it have something to do with:
> http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm
> 
> And if so can anyone explain it a bit for me, I'm being slow tonight.
> 
> I thought each class got it's own namespace and this sharing of mutable 
> objects is confusing me.

It has nothing to do with namespaces, it is the semantics of assignment 
that is your problem. What you have done is essentially the same as 
this, just obfuscated ;-)

In [1]: d = { "srv" : "why", "goo" : "sticky" }
In [2]: n = d
In [3]: n.pop('srv')
Out[3]: 'why'
In [4]: d
Out[4]: {'goo': 'sticky'}

Look at the references I sent (or wait for someone else with more energy 
than I to explain :-) )

Kent


More information about the Tutor mailing list