<div dir="ltr"><div>I have seen some interest into lazy functionality implementation.<br><br></div>I wondered if it can be linked with optional import.<br><div class="gmail-m_-2113992186940316494gmail-post-text">
<p><a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer" target="_blank">PEP 8</a> authoritatively states:</p>

<blockquote><div>
  <p>Imports are always put at the top of
  the file, just after any module
        comments and docstrings, and before module globals and constants.</p></div></blockquote>So, if we want to stick to PEP8 with non mandatory import, we have to catch the import errors, or jail the class or function using extra functionnality.<br><br></div><div class="gmail-m_-2113992186940316494gmail-post-text">Why not using the potential lazy keyword to have a nice way to deal with it?<br><br></div><div class="gmail-m_-2113992186940316494gmail-post-text">For example:<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>lazy import pylab as pl  # do nothing for now<br><br></div><div># do stuff<br><br></div><div>def plot(*args):<br></div><div>    pl.figure() # Will raise an ImportError at this point<br></div><div>    pl.plot(...)<br></div></blockquote><div><br></div><div>That way, our library will raise an ImportError only on plot func usage with an explicit traceback : if matplotlib is not installed, we will have the line where it is used for the first time and we will have the name of the faulty library.<br></div></div>