You only get the ease-of-implementation benefit if you are willing to
explicitly mark every _use_ of a lazy-imported name as special (and
give the fully qualified name at every usage site). This is rather
more cumbersome (assuming multiple uses in a module) than just
explicitly marking an import as lazy in one location and then using
the imported name in multiple places normally.

Other "lazy import" solutions are trying to solve a problem where you
want the name to be usable (without special syntax or marking) in many
different places in a module, and visible in the module namespace
always -- but not actually imported until someone accesses/uses it.
The difficulty arises because in this case you need some kind of
placeholder for the "deferred import", but you need to avoid this
"deferred object" escaping and becoming visible to Python code without
being resolved first. Explicitly marking which imports are lazy is
fine if you want it (it's just a matter of syntax), but it doesn't do
anything to solve the problem of allowing usage of the lazy-imported
name to be transparent.

I agree that the idea that top-of-module imports help readability is
overstated; it sounds slightly Stockholm-syndrome-ish to me :)
Top-of-module imports are frankly a pain to maintain and a pain to
read (because they are often distant from the uses of the names). But
they are a necessary evil if you want a) namespaces and b) not
constantly retyping fully-qualified names at every usage site. Python
is pretty committed to namespaces at this point (and I wouldn't want
to change that), so that leaves the choice between top-of-module
imports vs fully qualifying every use of every name; pick your poison.
(Inline imports in a scope with multiple uses are a middle ground.)

Carl
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/N4T2YMPHBLJXKCFA5CIPBFIZJJKO7SHR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to