Skip to content

Commit fd79af7

Browse files
authored
Doc: Try to enhance wording on circular imports. (GH-24705)
1 parent eb77133 commit fd79af7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Doc/faq/programming.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,26 +1898,26 @@ How can I have modules that mutually import each other?
18981898

18991899
Suppose you have the following modules:
19001900

1901-
foo.py::
1901+
:file:`foo.py`::
19021902

19031903
from bar import bar_var
19041904
foo_var = 1
19051905

1906-
bar.py::
1906+
:file:`bar.py`::
19071907

19081908
from foo import foo_var
19091909
bar_var = 2
19101910

19111911
The problem is that the interpreter will perform the following steps:
19121912

1913-
* main imports foo
1914-
* Empty globals for foo are created
1915-
* foo is compiled and starts executing
1916-
* foo imports bar
1917-
* Empty globals for bar are created
1918-
* bar is compiled and starts executing
1919-
* bar imports foo (which is a no-op since there already is a module named foo)
1920-
* bar.foo_var = foo.foo_var
1913+
* main imports ``foo``
1914+
* Empty globals for ``foo`` are created
1915+
* ``foo`` is compiled and starts executing
1916+
* ``foo`` imports ``bar``
1917+
* Empty globals for ``bar`` are created
1918+
* ``bar`` is compiled and starts executing
1919+
* ``bar`` imports ``foo`` (which is a no-op since there already is a module named ``foo``)
1920+
* The import mechanism tries to read ``foo_var`` from ``foo`` globals, to set ``bar.foo_var = foo.foo_var``
19211921

19221922
The last step fails, because Python isn't done with interpreting ``foo`` yet and
19231923
the global symbol dictionary for ``foo`` is still empty.

0 commit comments

Comments
 (0)