Confused with module and .py files

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Oct 5 14:44:21 CEST 2005


On Wed, 05 Oct 2005 13:46:30 +0530, 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, because the BaseClass from the second import over-writes the
BaseClass from the first.

In general, "from module import *" is a bad idea, because you don't know
what names you are importing: you can have name collisions, where a name
in one module clashes with a name in your code, or another module. That is
what is happening with your code.

The way to prevent that is to use Python's namespaces: instead of "from
module import name", use "import module", and then call module.name.


-- 
Steven.




More information about the Python-list mailing list