On 2/8/2012 3:16 PM, Brett Cannon wrote:
On Wed, Feb 8, 2012 at 14:57, Terry Reedy <tjreedy@udel.edu
    Would the following work? Treat a function as a 'loop' in that it
    may be executed repeatedly. Treat 'import x' in a function as what
    it is, an __import__ call plus a local assignment. Apply a version
    of the usual optimization: put a sys.modules-based lazy import
    outside of the function (at the top of the module?) and leave the
    local assignment "x = sys.modules['x']" in the function. Change
    sys.modules.__delattr__ to replace a module with a dummy, so the
    function will still work after a deletion, as it does now.

Probably, but I would hate to force people to code in a specific way for
it to work.

The intent of what I proposed it to be transparent for imports within functions. It would be a minor optimization if anything, but it would mean that there is a lazy mechanism in place.

For top-level imports, unless *all* are made lazy, then there *must* be some indication in the code of whether to make it lazy or not.

Not true; importlib would make it dead-simple to whitelist what modules to make lazy (e.g. your app code lazy but all stdlib stuff not, etc.).
">

(original) (raw)



On Wed, Feb 8, 2012 at 15:31, Terry Reedy <tjreedy@udel.edu> wrote:

On 2/8/2012 3:16 PM, Brett Cannon wrote:
On Wed, Feb 8, 2012 at 14:57, Terry Reedy <tjreedy@udel.edu
Would the following work? Treat a function as a 'loop' in that it
may be executed repeatedly. Treat 'import x' in a function as what
it is, an \_\_import\_\_ call plus a local assignment. Apply a version
of the usual optimization: put a sys.modules-based lazy import
outside of the function (at the top of the module?) and leave the
local assignment "x = sys.modules\['x'\]" in the function. Change
sys.modules.\_\_delattr\_\_ to replace a module with a dummy, so the
function will still work after a deletion, as it does now.

Probably, but I would hate to force people to code in a specific way for
it to work.

The intent of what I proposed it to be transparent for imports within functions. It would be a minor optimization if anything, but it would mean that there is a lazy mechanism in place.

For top-level imports, unless \*all\* are made lazy, then there \*must\* be some indication in the code of whether to make it lazy or not.

Not true; importlib would make it dead-simple to whitelist what modules to make lazy (e.g. your app code lazy but all stdlib stuff not, etc.).