Comment

Comment by kortex

I really hope a solution can be found and python can get true lazy imports (without doing the manual thing). I work in the ML/AI space and manual lazy imports are basically mandatory unless you want several seconds to see usage text from `footool --help`. Why "pay" for executing code you don't use?

Manual lazy import meaning:

    def uses_foo(x):
        import foo
        return foo.bar(x)
Manual lazy import sucks because:

- it's just ugly, I like imports all at the top so I can see all the deps

- bad for static analysis

- performance hit every time the function is called

Eschewing lazy imports has several problems:

- you always pay the execution cost, even if you don't use it

- also bad for static analysis and testing, since you have to eat the import time even if the code block you want to test doesn't execute the expensive path

- sometimes you need lazy import to avoid circular import errors

It's too bad that the main impediment to this is existing code which relies on side effects. Import-time side effects are an absolute pain in the ass. Avoid it at all costs.

parent kortex 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton