Message360629
| Author |
Maciej Gol |
| Recipients |
Maciej Gol, brett.cannon, eric.snow, ethan.furman, lars.gustaebel, ncoghlan, serhiy.storchaka |
| Date |
2020-01-24.16:50:23 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<CA+vMsyoFR=F3nhDAtKcvisFOG6713veAU=YF4n2c+8UTXSps4A@mail.gmail.com> |
| In-reply-to |
<1579874904.2.0.992346888571.issue39430@roundup.psfhosted.org> |
| Content |
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>
> _______________________________________
> |
|