[Tutor] dictionaries, objects and scoping...

John Morris jrmorrisnc at gmail.com
Tue Jan 22 04:04:31 CET 2008


Ahhh. so namespaces are scoped, not objects...
You have to keep your objects separate (and make copies when needed), Python
just keeps namespaces (names that refer to an object) scoped according to
it's rules:
http://docs.python.org/ref/naming.html

So if you create an object way up in terms of scope (global), then all
python does is handle what names are available in a given scope to refer to
it. If you want a separate object you have to take care of that yourself.
Efficient. Massive potential for gotchas, especially with some of python's
cleverness in terms of scoping rules.

Sort of. I see some light beginning to dawn ;).

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/e7934afb/attachment.htm 


More information about the Tutor mailing list