[Python-ideas] PEP 562

Guido van Rossum guido at python.org
Mon Sep 11 11:45:37 EDT 2017


There's no need for shame! I regularly find out that there are Python
features I didn't know about. It's called perpetual learning. :-)

On Sun, Sep 10, 2017 at 9:02 PM, INADA Naoki <songofacandy at gmail.com> wrote:

> Oh, I'm shame myself.
>
> Only when `from email import *` is used, __all__ submodules are imported.
> INADA Naoki  <songofacandy at gmail.com>
>
>
> On Mon, Sep 11, 2017 at 12:17 PM, Guido van Rossum <guido at python.org>
> wrote:
> > I don't think submodules are automatically imported, unless there are
> import
> > statements in __init__.py.
> >
> > On Sun, Sep 10, 2017 at 8:08 PM, INADA Naoki <songofacandy at gmail.com>
> wrote:
> >>
> >> It looks simple and easy to understand.
> >>
> >> To achieve lazy import without breaking backward compatibility,
> >> I want to add one more rule:  When package defines both of __getattr__
> and
> >> __all__, automatic import of submodules are disabled (sorry, I don't
> have
> >> pointer to specification about this behavior).
> >>
> >> For example, some modules depends on email.parser or email.feedparser.
> >> But since email/__init__.py uses __all__, all submodules
> >> are imported eagerly.
> >>
> >> See
> >> https://github.com/python/cpython/blob/master/Lib/email/
> __init__.py#L7-L25
> >>
> >> Changing __all__ will break backward compatibility.
> >> With __getattr__, this can be lazy import:
> >>
> >> import importlib
> >>
> >> def __getattr__(name):
> >>     if name in __all__:
> >>         return importlib.import_module("." + name, __name__)
> >>     raise AttributeError(f"module {__name__!r} has no attribute
> {name!r}")
> >>
> >>
> >> Regards,
> >>
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> https://mail.python.org/mailman/listinfo/python-ideas
> >> Code of Conduct: http://python.org/psf/codeofconduct/
> >>
> >
> >
> >
> > --
> > --Guido van Rossum (python.org/~guido)
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170911/ec619ca3/attachment.html>


More information about the Python-ideas mailing list