Confused with module and .py files

I

Iyer, Prasad C

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 *





regards
prasad chandrasekaran










--- Cancer cures smoking

#-----Original Message-----
#From: [email protected]
#[mailto:p[email protected]] On
#Behalf Of (e-mail address removed)
#Sent: Wednesday, October 05, 2005 1:32 PM
#To: (e-mail address removed)
#Subject: Python-list Digest, Vol 25, Issue 65
#
#Send Python-list mailing list submissions to
# (e-mail address removed)
#
#To subscribe or unsubscribe via the World Wide Web, visit
# http://mail.python.org/mailman/listinfo/python-list
#or, via email, send a message with subject or body 'help' to
# (e-mail address removed)
#
#You can reach the person managing the list at
# (e-mail address removed)
#
#When replying, please edit your Subject line so it is more specific
#than "Re: Contents of Python-list digest..."

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
 
R

Roel Schroeven

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 *

Name conflicts like that are a good reason not to use from ... import *,
but instead:

import import1
import import2

bc1 = import1.BaseClass()
bc2 = import2.BaseClass()

(Note: don't include the extension .py in the import statements)

Namespaces are great for preventing name conflicts; don't circumtvent
them by blindly importing everything into the same namespace. As the Zen
of Python says: "Namespaces are one honking great idea -- let's do more
of those!"
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top