Re:Optimizations which made Python 3.6 faster than Python 3....
by marmaduke on Friday June 09, 2017 @02:55PM
(#14523540)
It'd be pretty easy to do the experiment, since you can override the import logic and do lazy imports, eg.
class LazyModule:
def __init__(self, name):
self.name = name
def __getattr__(self, key):
mod = {}
exec('import %s as mod', mod)
self.mod = mod['mod']
return getattr(self.mod, key)
class LazyImporter:
def get_module(self, name):
return LazyModule(name)
Re:Optimizations which made Python 3.6 faster than Python 3....
by deckiedan on Tuesday June 06, 2017 @10:44PM
(#14503314)
That's kind of what I was hoping. I suspect a vast proportion of normal Python code doesn't use the meta programming and run on import and so on.
Re:Optimizations which made Python 3.6 faster than Python 3....
by joshuamorton on Wednesday June 07, 2017 @02:47AM
(#14504183)
If a module imports anything, it cannot be assumed clean, since any of its imports may execute code, and they will not be available at compile time.
In any case, given your points, it seems like a future Python version ought to introduce a new version of the import statement with lazy semantics (which, besides eliminating dead code, is also IMO the more correct/explicit behavior when importing symbols).