[Tutor] Importing Modules

Kent Johnson kent37 at tds.net
Tue Apr 25 12:31:31 CEST 2006


John Connors wrote:
> G'day,
> 
> I'm having trouble understanding the difference between,
> 
> import sys
> and
> from sys import *

The second style is strongly discouraged. As Alan pointed out, it can 
lead to surprises when you import more than you expect. I was once 
surprised to find out that when I write something like

a.py
####
foo = 1
bar = 2

b.py
####
from a import *

c.py
####
from b import *

Now foo and bar are defined in module c!!

The other reason to avoid this style is it removes clues about where a 
name is defined. If in module c above you want to know where foo comes 
from, it would be hard to find out. On the other hand if I wrote

from a import foo

then a search within c would show me where foo is defined.

Kent



More information about the Tutor mailing list