4 ms·
This is mortifying. This should not be acceptable implicit behaviour for imports to implicitly run code by simply existing
by conradludgate 2y ago
This is mortifying. This should not be acceptable implicit behaviour for imports to implicitly run code by simply existing
- DonHopkins 2y agoI know, right??! I was alternating between hitting my head up against the wall in despair, and jumping for joy I accidentally found a way to speed up detectron2 by 10%. I'll take the win. YOLO: You Only Load Once
- buildbot 2y agoThat’s just the way Python works though, an import reads a script line by line defining the functions and executing calls. I think that’s true of any scripting language?
- DonHopkins 2y agoHow it works and how it should be used are different. Say “no” to import side‐effects in Python: https://news.ycombinator.com/item?id=7536246 https://news.ycombinator.com/item?id=7536246 https://chrismorgan.info/blog/say-no-to-import-side-effects-in-python https://chrismorgan.info/blog/say-no-to-import-side-effects-... >I must be able to import your module— any module— at any time without anything breaking. Or even the beneficial side effect of some other module running 10% faster. ;) What side-effects, if any, are okay when importing a python module? https://softwareengineering.stackexchange.com/questions/454069/what-side-effects-if-any-are-okay-when-importing-a-python-module https://softwareengineering.stackexchange.com/questions/4540... >Q: Is this a better design pattern or does it just kick the issue down the road? >A: Kicking the issue down the road is the basic idea of the "functional core, imperative shell" design pattern. Functional Core, Imperative Shell (2012): https://news.ycombinator.com/item?id=34860164 https://news.ycombinator.com/item?id=34860164 https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell https://www.destroyallsoftware.com/screencasts/catalog/funct...
- buildbot 2y agoWell yeah, I wouldn't hold it that way either. The number of PRs I've commented on to take side effects out from imports is like , 75% of all the PRs I've seen in my current job. Maybe Python 4 can make anything except a function definition or import be a syntax error in a module haha.
- zbentley 2y agoC libraries compiled with -ffast-math would like a word…