On 10/04/2022 15:52, Guido van Rossum wrote:
On Sun, Apr 10, 2022 at 2:31 AM Daniel Pope <lord.ma...@gmail.com> wrote:
On Fri, 8 Apr 2022, 17:44 Guido van Rossum, <gu...@python.org> wrote:
The interesting idea here seems to make "lazy imports" easier
to implement by making them explicit in the code. So far, most
lazy import frameworks for Python have done hacks with
`__getattribute__` overrides.
The value is more than ease of implementation. Having syntax for
import expressions makes them statically analysable, which is
needed for type checkers and IDE autocompletion.
This has been brought up a few times and I don't get it. Currently a
use of an imported module is perfectly analyzable by all the static
type checkers I know of (e.g. mypy, Pyre, pyright). For the static
analyzer it makes no difference if I have
import re
.
.
.
def foo(x):
if re.match(r"blah", x): ...
or the hypothetical inline form:
def foo(x):
if @re.match(r"blah", x): ...
But I also see value in being able to type out code that uses
modules not yet imported without breaking my flow to add an import
statement. I don't yet trust IDEs to do this because I've been
bitten by them doing so incorrectly in the past.
I have too.
I see no reason why @re.match(...) couldn't just be equivalent to
__import__("re").match(...) (or a more optimised compiled version of
that). Any side-effects of importing the module (of which there probably
shouldn't be any for most modules) just happen at whenever the first
time the @re.match(...) is evaluated.
After the first call, and unless __import__ is overloaded, I also don't
think @re.match(...) would have to be significantly slower than
re.match(...). The latter already has to look up a global variable
(assuming the module was imported globally, which is almost always the
case). I don't think it would be too hard to optimise and Quicken.
_______________________________________________
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/3P5KUDUZXOIEZSEWNV7UMVAQNGYPGXDF/
Code of Conduct: http://python.org/psf/codeofconduct/