import and global trouble

Alex Martelli aleax at aleax.it
Fri Oct 10 10:01:46 EDT 2003


dag4004 wrote:
   ...
> Is there some thing special to know when we use from --- import *

Yes: the short form is "don't".

Always use 'import' (often 'import somelongname as short') and
live happily ever after.

The long form is: "from X import *" assigns a bunch of names
from X (all listed in __all__, or if that's missing all that
don't start with '_') in the current module.  Just like assignment,
when you do:

bar1 = 'egg'
bar2 = bar1
bar1 = 'bob'

bar2 is still 'egg', NOT 'bob'.  There is no connection between
bar1 and bar2 except that historically at some point they just
happened to reference the same object (due to "bar2 = bar1").

Just the same applies to "from X import *" -- there is no
connection between the names you're assigning in the local
module and those in X, except that at this very moment each
pair happens to reference the same object, just as if you
had assigned them one by one (except that you have no control
on the process, so it's quite accident-prone).


Alex





More information about the Python-list mailing list