On Fri, Mar 12, 2021 at 8:10 AM Matt del Valle <matthew...@gmail.com> wrote: > > I don't really have any knowledge of the Python interpreter's internals so > I'm afraid any suggestions of mine on implementation details are going to be > shots in the dark. > > That said, my intuition tells me that something similar to the proxy object > pattern currently used by many lazy import implementations maybe could work > here, with the interpreter creating some sort of 'lazy import proxy' object > on this line rather than performing the import: > > > from .extremely_expensive_module lazy import SomeClass > > And then when it actually is used in any way as part of any code that isn't > another lazy import statement (normal import statements and even assignment > statements that don't modify the object or attempt to access its attributes > would count) it actually gets fully imported at that point in time, and all > references anywhere in the application to this 'lazy import proxy' are > swapped to point at the real object. > > You could have it so that if you tried to lazy import an object that is > actually already a 'lazy import proxy' you would just get a reference to it > rather than provisioning a new one. This would allow you to 'chain' lazy > import statements across several modules to defer the import until the object > (be it a module or a class) is actually used. >
Seems fairly reasonable, I think. I'm still not convinced of the real value of this, given that you can always just import something inside a function or somesuch, and repeated imports are always going to be cheap; it's going to come down to use-cases. But if lazy imports were to be a thing, the semantics you describe aren't bad. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/CM75CSYD4YVFPJ44HZUD7HUHU2EVXIED/ Code of Conduct: http://python.org/psf/codeofconduct/