[Tutor] dictionaries, objects and scoping...
Kent Johnson
kent37 at tds.net
Tue Jan 22 04:18:14 CET 2008
John Morris wrote:
> Ahhh. so namespaces are scoped, not objects...
I would say names are scoped, but I guess namespaces are too.
> You have to keep your objects separate (and make copies when needed),
Yes
> 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.
Yes. Assignment is not copying, it is name-binding.
> Massive potential for gotchas, especially with some
> of python's cleverness in terms of scoping rules.
In my experience it is rare to actually need a copy of an object, and
usually pretty clear when I do. The real hurdle is getting your mental
model in sync with what Python is actually doing, rather than what you
think it should be doing.
Kent
More information about the Tutor
mailing list