[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Importing Modules

Gabriel Genellina

3/11/2010 1:37:00 AM

En Wed, 10 Mar 2010 09:19:29 -0300, PEYMAN ASKARI <paskari007@yahoo.ca>
escribió:

> I wanted to know if imported classes are treated differently than
> internal classes.

If by 'internal classes' you mean 'classes defined in the main module',
no, they're absolutely the same.

> class A():
> __init__(self):
> pass
>
> and call A, or A() I get something to the effect of <__main__.A
> instance at 0xb7f088ac>
>
> but if I call from module_name import A and then call A or A() I get
> something to the effect of <module_4.module_4 instance at 0xb7f0882c>

Note that both reprs have the same structure. Both say
<modulename.classname instance at ...>
When you execute `python test_import.py`, the code is executed into a
newly created module called __main__ (not test_import). So __main__ is the
name of the main module, and __main__.A is the full name of the A class
defined in the __main__ module.
In the other case, module_4.module_4 is the full name of the module_4
class defined in the module_4 module.

> I was wondering if this would be a problem.

In general, no, you shouldn't care. There is a problem, though, if some
other module imports the main one (using `import test_import`). Python
knows the main one as "__main__", not as "test_import", so it will load it
*again*, and you'll end with two separate copies of the same module.
Solution: don't do that :) -- redesign your application so subordinate
modules don't have to import the main one (move the required parts into a
third module).

--
Gabriel Genellina