Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Note the PEP does have a FAQ entry that mentions reasons they believe this proposed solution might be preferable to LazyLoader

https://pep-previews--4622.org.readthedocs.build/pep-0810/#f...

Q: Why not use importlib.util.LazyLoader instead?

A: LazyLoader has significant limitations:

Requires verbose setup code for each lazy import.

Has ongoing performance overhead on every attribute access.

Doesn’t work well with from ... import statements.

Less clear and standard than dedicated syntax.



Thanks for pointing it out!

> Has ongoing performance overhead on every attribute access.

I would have expected so, but in my testing it seems like the lazy load does some kind of magic to replace the proxy with the real thing. I haven't properly dug into it, though. It appears this point is removed in the live version (https://peps.python.org/pep-0810).

> Doesn’t work well with from ... import statements.

Hmm. The PEP doesn't seem to explain how reification works in this case. Per the above it's a solved problem for modules; I guess for the from-imports it could be made to work essentially the same way. Presumably this involves the proxy holding a reference to the namespace where the import occurred. That probably has a lot to do with restricting the syntax to top level. (Which is the opposite of how we've seen soft keywords used before!)

> Requires verbose setup code for each lazy import.

> Less clear and standard than dedicated syntax.

If you want to use it in a fine-grained way, then sure.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: