2 ms·
It's also worth stressing that even if import side-effects are always going to be fast and not kill the interpreter, they are still a terrible idea. Even spawn
by dpwm 13y ago
It's also worth stressing that even if import side-effects are always going to be fast and not kill the interpreter, they are still a terrible idea.
Even spawning objects that are referenced in modules can have some rather unpleasant properties. The __del__ method will likely never get reliably called and other behaviours that work great in scripts break in subtle ways, especially with a KeyboardInterrupt. Threading and multiprocessing will leave processes running using 100% cpu. Trying to debug these things gets insane, as you can often only find them as the interpreter is dying.
I think import side-effects can be tempting because Python is often introduced using a scripting-oriented approach. Combined with Python following the principle of least surprise, most people doing this won't even realise that it's wrong. This does seem to be a rather common anti-pattern.