The good "import" convention?

newgene newgene at bigfoot.com
Fri Feb 22 15:35:13 EST 2002


Hi, group,
     When I write a python module, I got a question about the location
of "import" statement.

1. import all needed modules in the top of the code. Some modules
might be used in only one function.

import module1,module2,...
import ...
def f1():
   ....
def f2():
   ....

2. import needed modules in each functions. Some common modules like
"os", might be imported in multiple functions.

def f1():
   import module1
   ...
def f2():
   import module1,modules2
   ...

Which one is better or they are the same for the performance?


Thanks.

NewGene



More information about the Python-list mailing list