Namespace Problem with global declaration in module
gmcm at hypernet.com
Thu Apr 27 09:23:52 EDT 2000
spex66 at my-deja.com wrote:
> hmm, deja is not so fast as my newsreader :), so here is a reply
> of Gordons message
> >spex66 at my-deja.com wrote:
> >> Hi,
> >> it seemed very simple but don't worked like expected:
> >> #FILE: test.py
> >> dd = None #INIT a shadow instance for automated access
> >> >>> from test import *
> >There's your problem. "from test import *" copies names from
> >test to your namespace. Now dd is another name for None. yy()
> >affects the dd in test's namespace, not yours.
> >- Gordon
> I see the point, but I like the from statement :)
Tough :-). import * is mostly a shortcut for interactive work. Look at the
top of Tkinter.py to see what a module needs to do to make import *
> Is it a bug?
> It's not the way I expected this code to work... and
> I cannot find a hook in the documentation that explicit explained
> this misbehaviour.
It's there, in 6.11 of the Language Ref:
The from form does not bind the module name: it goes through the
list of identifiers, looks each one of them up in the module found in
step (1), and binds the name in the local namespace to the object
thus found. If a name is not found, ImportError is raised. If the list
of identifiers is replaced by a star ("*"), all names defined in the
module are bound, except those beginning with an underscore ("_").
No, the implications of that statement are not immediately obvious.
> Any idea for a workaround (I need the from-statement for further
> code readability)?
No, you want it for type-ability. Come back to it in a few months and
you'll wish you'd spelled out where all those names came from.
More information about the Python-list