advice on this little script

Carl Banks invalidemail at aerojockey.com
Thu Mar 9 13:23:33 EST 2006


John Salerno wrote:
> John Salerno wrote:
>
> > from time import sleep
> ...
> > 	sleep(1.0)
>
> Very picky point, but I'd like to know what others think of this. Should
> I import as above, or should I do this:
>
> import time
> ....
> time.sleep(60.0)   ???
>
> I think the 'from time import sleep' looks cleaner, because I'm only
> taking what I need (is an import any more expensive than this from?),
> but I also feel like the 'time.sleep' syntax is much more
> self-describing and better to read than just 'sleep'.
>
> So what do you guys think between these two choices?

I used to do both, depending on what was more convenient.  These days,
as I've been writing larger and more complex programs, I pretty much
just import the module.  I find that a consistent approach just keeps
things easier to digest, gives me less to think about and fewer
decisions to make.  One can consistently import the module, but not the
symbols within because sometimes there's just too many.

The exception is I still import global singleton objects using
from...import.  Such objects behave more like modules in my code
anyways, and probably they'd actually be modules if not for the
ugliness of using the global statement.

Having said all that, I still cringe slightly when writing stuff like
glob.glob() and time.time().  (Which brings me to a related piece of
advice: a good way to name modules that avoids confusion with other
symbols is to use "act of" words, i.e., words that end in ing, ion,
age, or ment.  I would have named the glob module "globbing", the time
module "timing", and so on; then I wouldn't have to cringe.)


Carl




More information about the Python-list mailing list