Newbie Question

Brian Quinlan brian at sweetapp.com
Tue Sep 18 00:29:10 EDT 2001


Mel wrote:
> > the frequent use of "from x import *" statements should be avoided
> 
> Could you elaborate on why this is a problem, given that I have ample
RAM
> in my computer?

It can be a problem for the person reading your code, e.g.:

from x import *
from y import *
from z import *
# More of the above

q() # where did this come from?

It can also pollute your namespace with a lot of names that aren't
relevant or even interfere with each other e.g. 

from cmath import *
from math import *

sin(2j+2) # TypeError because math's sin implementation overwrote
cmath's

> It's still not clear to me. When I import a module (e.g., math) and
then
> use an expression (e.g., sin), why can Python not then find something 
> with the name of sin? This is separate from whether Python also knows 
> how to process sin.

You could argue that is the correct behavior; it is how most programming
languages work. However, for the reasons that I mentioned above, I think
that it is best to be explicit about the symbols that you want in your
namespace.

Cheers,
Brian





More information about the Python-list mailing list