Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

It is intended to support circular imports. Let foo.py contains "import bar" 
and bar.py contains "import foo". When you execute "import foo", the import 
machinery first creates an empty module foo, adds it to sys.modules, reads 
foo.py and executes it in the namespace of module foo. When the interpreter 
encounters "import bar" in foo.py, the import machinery creates an empty module 
bar, adds it to sys.modules, reads bar.py and executes it in the namespace of 
module bar. When the interpreter encounters "import foo" in bar.py, the import 
machinery takes the module foo from sys.modules. So you break an infinite cycle 
and can import modules with cyclic dependencies.

You can argue that cyclic import does not look as a good practice, but actually 
it is pretty common case when you import a submodule in a package. If 
foo/__init__.py contains "from .bar import Bar", the foo module must be 
imported before you import foo.bar, but is not completely initialized at that 
time yet.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39430>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to