zoukankan      html  css  js  c++  java
  • python module circular import

    Cyclic imports terminate, but you need to be careful not to use the cyclically-imported modules during module initialization.

    Consider the following files:

    a.py:

    print"a in"import sys
    print"b imported: %s"%("b"in sys.modules,)import b
    print"a out"

    b.py:

    print"b in"import a
    print"b out"
    x =3

    If you execute a.py, you'll get the following:

    $ python a.py
    a in
    b imported:False
    b in
    a in
    b imported:True
    a out
    b out
    a out

    On the second import of b.py (in the second a in), the Python interpreter does not import b again, because it already exists in the module dict.

    Edit: If you try to access b.x from a during module initialization, you will get an AttributeError.

    Append the following line to a.py:

    print b.x

    Then, the output is:

    $ python a.py
    a in                    
    b imported:False
    b in
    a in
    b imported:True
    a out
    Traceback(most recent call last):File"a.py", line 4,in<module>import b
      File"/home/shlomme/tmp/x/b.py", line 2,in<module>import a
     File"/home/shlomme/tmp/x/a.py", line 7,in<module>print b.x
    AttributeError:'module' object has no attribute 'x'

    This is because modules are executed on import and at the time b.x is accessed, the line x = 3 has not be executed yet, which will only happen after b out.

  • 相关阅读:
    Python 准确获取今天是星期几的代码(isoweekday和weekday
    python对象之间的交互
    cpu的工作原理
    ubuntu禁止内核自动更新
    计算机组成原理
    python实例化对象过程
    python统计班级学生
    python私有变量和方法
    python变量 方法 属性
    制作再生龙启动盘
  • 原文地址:https://www.cnblogs.com/threef/p/3538990.html
Copyright © 2011-2022 走看看