Strange Result about Module circular rreference

CFAN eyelinux at gmail.com
Sun Feb 3 02:01:44 CET 2008


On 2月3日, 上午8时59分, CFAN <eyeli... at gmail.com> wrote:
> 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



More information about the Python-list mailing list