Strange Result about Module circular rreference

C

CFAN

First Set up the following files,

m1.py
---------------------
#Module A
import m2
var1 = "ModuleM1"

m2.py
---------------------
#Module B
import m3
var1 = "ModuleM2"

m3.py
---------------------
#Module C
import m1
var1 = "ModuleM3"

main.py
----------------------
import m3
print m3.var1
print m3.m1.var1
print m3.m1.m2.var1
print m3.m1.m2.m3.var1

Then to run main.py
the result is :

ModuleM3
ModuleA
8
Traceback (most recent call last):
File "D:\WorkTools\ActivePython\Lib\site-packages\pythonwin\pywin
\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\yoyo\My Documents\Script4.py", line
5, in ?
print m3.m1.m2.m3.var1
AttributeError: 'module' object has no attribute 'm3'

,can any one explain it, this test is inspired by Python Programming
which says that the moudle can reference in
the circular manner, and I got wired about it and then try this test.
the result that 'module' object has no attribute 'm3' seems okay ,if i
assumes that import only import a module once, but how about the reult
"ModuleA " and even "8" ?
 
C

CFAN

First Set up the following files,

m1.py
---------------------
#Module A
import m2
var1 = "ModuleM1"

m2.py
---------------------
#Module B
import m3
var1 = "ModuleM2"

m3.py
---------------------
#Module C
import m1
var1 = "ModuleM3"

main.py
----------------------
import m3
print m3.var1
print m3.m1.var1
print m3.m1.m2.var1
print m3.m1.m2.m3.var1

Then to run main.py
the result is :

ModuleM3
ModuleA
8
Traceback (most recent call last):
File "D:\WorkTools\ActivePython\Lib\site-packages\pythonwin\pywin
\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\yoyo\My Documents\Script4.py", line
5, in ?
print m3.m1.m2.m3.var1
AttributeError: 'module' object has no attribute 'm3'

,can any one explain it, this test is inspired by Python Programming
which says that the moudle can reference in
the circular manner, and I got wired about it and then try this test.
the result that 'module' object has no attribute 'm3' seems okay ,if i
assumes that import only import a module once, but how about the reult
"ModuleA " and even "8" ?

The result is got from Pythonwin Shell , and I'v try it from command
C:\Documents and Settings\yoyo\My Documents>python Script4.py
ModuleM3
ModuleM1
ModuleM2
it seems the it really works
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top