
On Fri, 8 Apr 2022 at 12:57, Daniel Pope <lord.mauve@gmail.com> wrote:
On Fri, 8 Apr 2022 at 12:23, Steve Dower <steve.dower@python.org> wrote:
I've read the rest of the thread so far, and agree strongly that we can't do this at the language/runtime level.
You mean the hoisting, right?
I don't see any reason why an import expression without hoisting would be impractical. But I'd like to hear your thoughts if you think it is.
Desirability on the other hand is subjective. I think I actually do desire it, others are not bothered. I don't see strong arguments as to why you definitely wouldn't want it in your language.
OK, I'll be explicit. I don't want this in Python. Having imports at the top of a file makes it much easier to see your dependencies. The current language features, combined with community wide style guides that discourage the use of local imports, make this straightforward, while still providing means of doing local imports if needed. This proposal isn't just about having a new syntax to do an on-demand import. It's also about normalising the idea that people can pull code from other modules without declaring the intent to do so up front. While I don't dispute that in some circumstances (notably the REPL, and throwaway scripts[1]) not having to add import statements would be nice, I don't see how we'd limit usage to cases like that - and I *really* don't want to have to work out where some weird inline import happened while debugging a priority 1 bug in a 10,000 line code base at 2am... Also, it should be possible to do something like this using the existing import machinery: from magic_import import on_demand_loader as OD ... # Many lines of code later default_args = { "start_date": OD.datetime.datetime(...) } Trivial proof of concept implementation: class OD: def __getattr__(self, name): return __import__(name) OD = OD() print(OD.datetime.datetime.now()) Paul [1] Of course, today's throwaway script usually turns out to be next month's key component in a mission-critical software stack :-(