Confused about namespaces

Chris Mellon arkanes at gmail.com
Fri Nov 18 18:47:40 EST 2005


On 18 Nov 2005 15:29:43 -0800, KvS <keesvanschaik at gmail.com> wrote:
> Ok, makes sense but didn't seem "natural" to me, although it is an
> obvious consequence of what you just pointed out, namely that modules
> are evaluated in their own namespace, something to keep in mind... On
> the other hand it does apparently work recursively "the other way
> around" since I didn't explicitly import wx in main.py but only
> indirect via importing GUIclasses in which wx is imported right?
>

it worked because all the code that used stuff from the wx namespace
was in GUIclasses.py. If you tried to use create wx classes directly
in your toplevel file it would fail unless you did an "import wx"
first.

Now, as a slight consequence of the specific way you imported, it may
appear to do something different - when GUIClasses.py imported wx, an
entry in the modules namespace was created with the value "wx", and
the value being the wx module. When you do "from GUIClasses import *",
that imports all the symbols in GUIClasses namespace into your local
namespace, including the "wx" entry. This kind of cascading behavior
is one reason that "from foo import *" is frowned up, because the
namespace pollution can cause confusing bugs.

> Thanks. :).
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list