On Wednesday, 7 June 2017 at 12:39:07 UTC, Russel Winder wrote:
Are there any idiom rules as to where to put import statements
in D?
In Python they can go anywhere but PEP-8 suggests they should
all go at the top of a file, just after the module
documentation string.
I don't know if there is any official dogma on how to use
imports, but given that local imports inside templates are only
loaded if the template is instantiated, there are benefits to
using local imports in those cases.
Another use for local imports is in unittests - any import that
is only used in unittests may benefit from being moved from
module scope to inside the unittests themselves, or for an import
used by many tests, a version (unittest) block.
In my own code, I use very loose guidelines but prefer to put any
imports that are used only in one or two functions, inside those
functions. Imports that are used by multiple functions are placed
at the top of the file, just below the module declaration (if
any).
Similarly, I try to use selective imports when only a few symbols
from a module are used in my module, but find that this becomes
unwieldy past 3-5 symbols.
--
Biotronic