Where to "import"?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Mar 8 15:23:15 EST 2007


Paulo da Silva a écrit :
> Hi!
> 
> If I have two files .py such as
> 
> m.py
> 	from c import *

avoid this kind of import except in an interactive interpreter and 
eventually in a package __init__.py. Better to use either:

   from c import c
or
   import c
   ...
   x = c.c()

> 	...
> 	x=c()
> 	...
> 	os.any_method ...

Then you need to import os

> 	...
> 
> c.py
> 	class c:
         class C(object):

1/ better to stick to naming conventions (class names in CamelCase)
2/ do yourself a favor: use new-style classes

> 		def __init__(self, ...):
> 			...
> 			os.any_method ...
> 			...
> 	...
> 
> both using os module where should I put the "import os"? In both files?

Yes. In your above example, it worked because of the "from c import *" - 
and this is exactly why it's bad form to use this in a module (well: 
that's one of the reasons why). Each module should *explicitly* import 
all it's direct dependencies.




More information about the Python-list mailing list