擅长:python、mysql、java
<p>一般指南如下:</p>
<blockquote>
<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>
</blockquote>
<p>也就是说,人们有时会将导入放在函数内部,在这种情况下,只有在特定函数调用的情况下才应该导入模块(通常是在模块不总是可用的情况下,或者为了避免循环导入,或者如果导入做了一些开销而需要推迟的操作)。例如,请参见<a href="https://github.com/toastdriven/itty/blob/master/itty.py#L804" rel="nofollow noreferrer">server adapters for the <em>itty</em> micro-webframework</a>。在</p>
<p>另一种做法是将一些导入放在仅在main部分中使用的模块的<code>if __name__ == '__main__'</code>之后。这可以防止在导入当前模块时不必要的子导入。在</p>