Message360629
This is a HUGE eye opener! Didn't know of that 'import' vs 'from x import
y' difference. Thanks a lot! Is it documented somewhere ?
pt., 24 sty 2020, 15:08 użytkownik Serhiy Storchaka <report@bugs.python.org>
napisał:
>
> Serhiy Storchaka <storchaka+cpython@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 <report@bugs.python.org>
> <https://bugs.python.org/issue39430>
> _______________________________________
> |
|
Date |
User |
Action |
Args |
2020-01-24 16:50:23 | Maciej Gol | set | recipients:
+ Maciej Gol, brett.cannon, ncoghlan, lars.gustaebel, ethan.furman, eric.snow, serhiy.storchaka |
2020-01-24 16:50:23 | Maciej Gol | link | issue39430 messages |
2020-01-24 16:50:23 | Maciej Gol | create | |
|