Lazy imports for Python
Lazy imports for Python
Posted Sep 8, 2022 8:20 UTC (Thu) by josh (subscriber, #17465)Parent article: Lazy imports for Python
Posted Sep 8, 2022 13:00 UTC (Thu)
by mathstuf (subscriber, #69389)
[Link]
Posted Sep 8, 2022 20:30 UTC (Thu)
by NYKevin (subscriber, #129325)
[Link] (2 responses)
This either doesn't work, or it's basically useless.
foo.py:
bar.py:
main.py:
Now, if you make main.py lazily import foo.py, then the side effects of bar.py will stop happening, which main might have accidentally relied on. So you would need to treat import statements as "statements other than declarations" - which effectively means that almost any nontrivial module will be eagerly imported. Or, alternatively, you have to recursively trace through all of the modules in the entire dependency tree and check each one for this lazy import flag - which is still pretty expensive and doesn't really save you all that much (consider the disk seeks!).
Posted Sep 14, 2022 6:39 UTC (Wed)
by arvidma (guest, #6353)
[Link] (1 responses)
This way, you only pay the cost once and any subsequent runs can take the fast path.
Posted Sep 15, 2022 8:54 UTC (Thu)
by smurf (subscriber, #17840)
[Link]
Lazy imports for Python
Lazy imports for Python
import bar
print("Side effect!")
import foo
Lazy imports for Python
Lazy imports for Python
