<div dir="ltr">Replying here, although this was written in response to the other thread:<div><br></div><div><div style="font-size:small">Hey Neil,</div><div style="font-size:small"><br></div><span style="font-size:small">In general this won't work. It's not generally possible to know if a given statement has side effects or not. As an example, one normally wouldn't expect function or class definition to have side effects, but if a function is decorated, the decorators are evaluated at function "compilation"/import time, and may have side effects. As another example, one can put arbitrary expressions in a function annotation, and those are evaluated at import time. </span></div><div><font size="2"><br></font></div><div><font size="2">As a result of this, you can't even know if an import is safe, because that module may have side effects. That is, the module foo.py:</font></div><div><font size="2"><br></font></div><div><font size="2">    import bar</font></div><div><font size="2"><br></font></div><div><font size="2">isn't known to be lazy, because bar may import and start the logging module, as an example. <br></font><div style="font-size:small"><br></div><div style="font-size:small">While in general you might think that global state modifying decorators or annotations are a bad idea, they are used (Flask). As a result, it's not possible to implement this without breaking behavior for certain users. While I'm not a core dev and thus can't say with certainty, I will say that that makes it very unlikely for this change (or others like it) to be implemented.</div></div><div style="font-size:small"><br></div><div style="font-size:small">It might be (from a language standpoint) to implement a `lazy` keyword (ie. `lazy import foo; lazy def bar(): ...`), but if I recall, there's been discussion of that and it's never gotten very far.</div><div style="font-size:small"><br></div><div style="font-size:small">--Josh</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Sep 7, 2017 at 1:46 PM Neil Schemenauer <<a href="mailto:nas@arctrix.com">nas@arctrix.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Barry Warsaw <<a href="mailto:barry@python.org" target="_blank">barry@python.org</a>> wrote:<br>
> There are a few other things that might end up marking a module as<br>
> "industrious" (my thesaurus's antonym for "lazy").<br>
<br>
Good points.  The analysis can be simple at first and then we can<br>
enhance it to be smarter about what is okay and still lazy load.  We<br>
may evolve it over time too, making things that are not strictly<br>
safe still not trigger the "industrious" load lazy anyhow.<br>
<br>
Another idea is to introduce __lazy__ or some such in the global<br>
namespace of the module, if present, e.g.<br>
<br>
__lazy__ = True<br>
<br>
then the analysis doesn't do anything except return True.  The<br>
module has explicitly stated that side-effects in the top-level code<br>
are okay to be done in a lazy fashion.<br>
<br>
Perhaps with a little bit of smarts in the analsis and a little<br>
sprinkling of __lazy__ flags, we can get a big chunk of modules to<br>
lazy load.<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>