Brian van den Broek wrote:


Hi Dave, Kent, and all,

I have a caution about the
 from Config import *
idiom that Kent didn't mention.

It can lead to namespace pollution, in that if you have a module 'foo' with a name 'bar' and you are witting a script which says
from foo import *
you have to be very careful that your script doesn't also assign to the name 'bar', else you may end up thinking you have two different things available when you don't. ('bar' will either point to your script's bar or to Config.bar, depending on whether you imported Config before or after your scripts assignment to bar.)


The first time this bites you, it can eat up hours of your life. (But I'm not bitter;-)

I avoid this by using the
 import examplemodule as em

That imports everything so that you accesses it by
 em.some_name
rather than
 examplemodule.some_name

I find that really handy for the handful of utility modules I import into most of my scripts. Then, I just have to be sure to avoid a small set of names -- 'em' in this case. And my python files have nice descriptive names, but I only have to type then once.

Best,

Brian vdB

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Thanks for pointing that out. Im a bit of a Python coward and opted for

from config import data_dir,HTML_addr

Mainly so I can see where these variables come from. I have never seen the 'as' operator on an import before, so much to learn (and remember) ;-)

Dave

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to