From: PEYMAN ASKARI on 10 Mar 2010 07:19 Hello I frequent the PyGtk mailing list all the time, but this is the first time I am posting here =) I wanted to know if imported classes are treated differently than internal classes. I have attached a minimal example which points out what I mean. Essentially, if I create a class: 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> I was wondering if this would be a problem. I have included the test_import.py file as well as 4 module files which it loads. Save the tes_import.py file to a folder, create another folder there, and save ONLY the 4 modules files to that new folder. In the code, update line 26 to reflect to path of the newly created directory Thanks in advanced Peyman
From: Terry Reedy on 10 Mar 2010 09:32 On 3/10/2010 7:19 AM, PEYMAN ASKARI wrote: > Hello > > I frequent the PyGtk mailing list all the time, but this is the first > time I am posting here =) > > I wanted to know if imported classes are treated differently than > internal classes. > > I have attached a minimal example which points out what I mean. > Essentially, if I create a class: > > 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> > > I was wondering if this would be a problem. Only if you code cares about A.__module__ (3.x, not sure of 2.x) and the string returned by str or repr, which nearly always, it should not. tjr
From: Gabriel Genellina on 10 Mar 2010 20:37 En Wed, 10 Mar 2010 09:19:29 -0300, PEYMAN ASKARI <paskari007(a)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
|
Pages: 1 Prev: Down casting Python objects Next: Can't define __call__ within __init__? |