Re: Confused with module and .py files
Iyer, Prasad C wrote: Actually I am bit confused between the modules and .py file How do I differentiate between the 2. A module 'name' is the same as the name of your file without the '.py' extension. 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 should say : from import1 import * from import2 import * If according to your earlier question, you have a class with the same name in two different modules, the better thing then (as others on the list have already pointed out) is to do the following: import import1, import2 c1 = import1.MyClass() c2 = import2.MyClass() regards, Satchit Satchidanand Haridas (sharidas at zeomega dot com) ZeOmega (www.zeomega.com) Open Minds' Open Solutions regards prasad chandrasekaran --- Cancer cures smoking #-Original Message- #From: [EMAIL PROTECTED] #[mailto:[EMAIL PROTECTED] On #Behalf Of [EMAIL PROTECTED] #Sent: Wednesday, October 05, 2005 1:32 PM #To: python-list@python.org #Subject: Python-list Digest, Vol 25, Issue 65 # #Send Python-list mailing list submissions to # python-list@python.org # #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 # [EMAIL PROTECTED] # #You can reach the person managing the list at # [EMAIL PROTECTED] # #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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused with module and .py files
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 * 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! -- If I have been able to see further, it was only because I stood on the shoulders of giants. -- Isaac Newton Roel Schroeven -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused with module and .py files
Iyer, Prasad C wrote: How would I use it if I declare it as given below in my 3rd class from import1.py import * from import2.py import * thats a syntax error; I assume you meant from import1 import * from import2 import * which simply doesn't work if you need to access things that happens to have the same name in both modules. it's like typing a = 1 a = 2 and then asking how you can access the original 1 via the a variable. so unless you know *exactly* what you're doing, you should *never* use from import * -- unless you're using a module that someone else wrote, and the documentation for that module tells you do use it. see http://effbot.org/zone/import-confusion.htm for more on this. (see other replies for how to do what you want in a way that actually works). /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused with module and .py files
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/ -- http://mail.python.org/mailman/listinfo/python-list
RE: Confused with module and .py files
Sorry guys for that .py in import. It was typing mistake. Extremely sorry regards prasad chandrasekaran --- Cancer cures smoking #-Original Message- #From: Steve Holden [mailto:[EMAIL PROTECTED] #Sent: Wednesday, October 05, 2005 2:43 PM #To: python-list@python.org #Subject: Re: Confused with module and .py files # #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/ # 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused with module and .py files
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. -- http://mail.python.org/mailman/listinfo/python-list