Efficiency/style issues of import <module> vs. from <module> import <name>, ...

Ethan Furman ethan at stoneleaf.us
Thu Jun 17 13:51:52 EDT 2010


Jack Diederich wrote:
> You want to import a name that is itself a namespace; preferably a
> module or package and sometimes a class.  Importing constants can lead
> to trouble.  ex/
> 
> from settings import DEBUG
> if DEBUG: log('debug is on!')
> 
> The value of the flag gets fetched at import time.  If code in another
> module updates settings.DEBUG later your module won't see it. ditto
> for exceptions.
> 
> -Jack

The idea behind constants is that they are... um... constant.  ;)  I 
imagine exceptions don't get changed often either.  (At least, I never 
change mine.)

If you have a setting that may change, don't call it a constant and name 
it accordingly.

~Ethan~



More information about the Python-list mailing list