[Tutor] Import Statement Differences

Magnus Lycka magnus@thinkware.se
Mon Jan 27 18:09:02 2003


At 10:44 2003-01-27 -0800, Danny Yoo wrote:
>The 'from time import *' statement will take everything that's in the
>'time' module, and dump all of it on our feet.

A middleground between 'import time' and 'from time import *'
is to write 'from time import strftime, localtime'. This means
that you just type 'localtime()' instead of 'time.localtime()'
when you use it, but you still don't clog your namespace with
any unexpected names.

I still suggest that you use "import time" though. As you code,
you will probably realize that you need to use time.time() and
time.clock() etc as well, and then you will need to go back to
the beginning of your program to add more functions to the
"from time import ..." line. This will be disruptive for your
programming. No flow there...

Also, when you read a program, seeing 'time.accept2dyear' is
much more informative than just seeing 'accept2dyear'. In the
second case, there is no way you can know from the code where
'accept2dyear' came from. In the first case it's clear that
it's from the time module. Sometimes it might be obvious where
a function or class belongs, but far from always.

Typing "time.localtime()" is not so horribly long, but what if your
module name is called MyHomemadeOracleNineDatabaseAdapter?

import MyHomemadeOracleNineDatabaseAdapter
conn = MyHomemadeOracleNineDatabaseAdapter.conn(connectstring)
cur = ...

Yuk!

This time we MUST use
   from MyHomemadeOracleNineDatabaseAdapter import *
right?

No, no, no! Just use the "as" part of the import statement!

import MyHomemadeOracleNineDatabaseAdapter as db

conn = db.conn(connectstring)
cur = ...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se