Confused with module and .py files

Steve Holden steve at holdenweb.com
Wed Oct 5 05:13:26 EDT 2005


Iyer, Prasad C wrote:
> Actually I am bit confused between the modules and .py file
> How do I differentiate between the 2.
> 
> For example
> I have a file import1.py, import2.py file
> Which has few functions and classes
> And if I have a class with same name "BaseClass" in both the file
> 
> How would I use it if I declare it as given below in my 3rd class
> 
> from import1.py import *
> from import2.py import *
> 
You can't do that. The "from module import *" mechanism explicitly 
defines names in the importing module's namespace, so if you use this 
technique to import two modules that define the same name you will 
inevitably find that the second import overwrites the duplicate name 
imported by the first import.

Note also that the ".py" should not be included in the import statement 
- the interpreter finds the appropriate code from the module name, so 
you should anyway be doing something like

   from import2 import *
   from import2 import *

It would be much better, though, to write:

   import import1
   import import2

Then you can refer to import1.BaseClass and import2.baseClass without 
getting any naming conflicts. In general the "from module import *" form 
should only be used under specific conditions, which we needn't discuss 
here now.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list