<div dir="ltr">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. :-)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 10, 2017 at 9:02 PM, INADA Naoki <span dir="ltr"><<a href="mailto:songofacandy@gmail.com" target="_blank">songofacandy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Oh, I'm shame myself.<br>
<br>
Only when `from email import *` is used, __all__ submodules are imported.<br>
INADA Naoki  <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>><br>
<div class="HOEnZb"><div class="h5"><br>
<br>
On Mon, Sep 11, 2017 at 12:17 PM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
> I don't think submodules are automatically imported, unless there are import<br>
> statements in __init__.py.<br>
><br>
> On Sun, Sep 10, 2017 at 8:08 PM, INADA Naoki <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>> wrote:<br>
>><br>
>> It looks simple and easy to understand.<br>
>><br>
>> To achieve lazy import without breaking backward compatibility,<br>
>> I want to add one more rule:  When package defines both of __getattr__ and<br>
>> __all__, automatic import of submodules are disabled (sorry, I don't have<br>
>> pointer to specification about this behavior).<br>
>><br>
>> For example, some modules depends on email.parser or email.feedparser.<br>
>> But since email/__init__.py uses __all__, all submodules<br>
>> are imported eagerly.<br>
>><br>
>> See<br>
>> <a href="https://github.com/python/cpython/blob/master/Lib/email/__init__.py#L7-L25" rel="noreferrer" target="_blank">https://github.com/python/<wbr>cpython/blob/master/Lib/email/<wbr>__init__.py#L7-L25</a><br>
>><br>
>> Changing __all__ will break backward compatibility.<br>
>> With __getattr__, this can be lazy import:<br>
>><br>
>> import importlib<br>
>><br>
>> def __getattr__(name):<br>
>>     if name in __all__:<br>
>>         return importlib.import_module("." + name, __name__)<br>
>>     raise AttributeError(f"module {__name__!r} has no attribute {name!r}")<br>
>><br>
>><br>
>> Regards,<br>
>><br>
>> ______________________________<wbr>_________________<br>
>> Python-ideas mailing list<br>
>> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
>><br>
><br>
><br>
><br>
> --<br>
> --Guido van Rossum (<a href="http://python.org/~guido" rel="noreferrer" target="_blank">python.org/~guido</a>)<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>