[Python-ideas] lazy import via __future__ or compiler analysis

Neil Schemenauer nas-python-ideas at arctrix.com
Mon Sep 11 14:09:27 EDT 2017


On 2017-09-11, C Anthony Risinger wrote:
> I'm not sure I follow the `exec(code, module)` part from the other thread.
> `exec` needs a dict to exec code into [..]
[..]
> How do you handle lazy loading when a defined function requests a global
> via LOAD_NAME? Are you suggesting to change function.__globals__ to
> something not-a-dict, and/or change LOAD_NAME to bypass
> function.__globals__ and instead do something like:

I propose to make function.__namespace__ be a module (or other
namespace object).  function.__globals__ would be a property that
calls vars(function.__namespace__).   Implementing this is a lot of
work, need to fix LOAD_NAME, LOAD_GLOBAL and a whole heap of other
things.  I have a partly done proof-of-concept implementation.  It
crashes immediately on Python startup at this point but so far I
have not seen any insurmountable issues.

Doing it while perserving backwards compatibility will be a
challenge.  Doing it without losing performance (LOAD_GLOBAL using
the fact that f_globals is an honest 'dict') is also hard.  It this
point, I think there is a chance we can do it.  It is a conceptual
simplification of Python that gives the language more consistency
and more power.

> All this chatter about modifying opcodes, adding future statements, lazy
> module opt-in mechanisms, special handling of __init__ or __getattr__ or
> SOME_CONSTANT suggesting modules-are-almost-a-class-but-not-quite feel like
> an awful lot of work to me, adding even more cognitive load to an already
> massively complex import system. They seem to make modules even less like
> other objects or types.

I disagree.  It would make for less cognitive load as LOAD_ATTR
would be very simlar to LOAD_NAME/LOAD_GLOBAL.  It makes modules
*more* like other objects and types.

I'm busy with "real work" this week and so can't follow the
discussion closely or work on my proof-of-concept prototype.  I hope
we can come up with an elegant solution and not some special hack
just to make module properties work.

Regards,

  Neil


More information about the Python-ideas mailing list