[Tutor] dictionaries, objects and scoping...

John Morris jrmorrisnc at gmail.com
Tue Jan 22 03:24:52 CET 2008


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.

Thanks.


On Jan 21, 2008 9:16 PM, John Morris <jrmorrisnc at gmail.com> wrote:

> class Foo:
>   '''Represents a foo'''
>   def __init__(self, name):
>     '''Initializes the person's data'''
>     self.name = name
>     print '(Initializing %s)' % self.name
>     self.ot = Bar(self.name)
>     print '(After Other - %s)' % self.name
>
> class Bar:
>     def __init__(self, name):
>         self.name = name
>         print 'Other', self.name
>         self.name.pop('srv')
>         print 'Other (Changed)', self.name
>
> dict = { "srv" : "why", "goo" : "sticky" }
> foo = Foo(dict)
> print foo.name
>
>
> Why does the pop in the Bar class nuke the srv k & v from Foo.name as
> well?
>
>


-- 
John Morris
jrmorrisnc at gmail.com
"Do nothing which is of no use." -- Miyamoto Musashi
<a href="http://profile.mygamercard.net/nerdality">
<img src="http://card.mygamercard.net/gbar/abyss/nerdality.gif" border=0>
</a>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080121/0cf99e0c/attachment.htm 


More information about the Tutor mailing list