Several times I met a mention that executing code when importing a module is a bad practice. At the same time, I could not find an explanation for this statement anywhere. Question two.
First, why is it bad practice to execute code when a module is imported?
Second: what is considered code execution, in the context of this statement? Assigning values to global variables, executing functions, or something else? After all, the definition of classes and functions in this module, as well as the import of other modules (in this module that we are importing), can this also be considered "code execution"?
I would not say that this is really a bad practice in general, you just need to approach this matter wisely, understand what it is fraught with, and do not use it thoughtlessly.
Reasons for limiting this case, if I think about it, I see the following:
Explicit is better than implicit . You lose control over exactly when the given code is executed, which can be important. Modules in Python can be imported not only at the top level, but also inside a function or method, which means that the module code will be executed not at the start of the application, but at the time of launching such a function and executing the
importstatement in it. Moreover, there may be more than one function that imports a module, and which of them and when is executed first in your huge application may not be obvious at all.
Despite the fact that the module code is executed once on the first load (in fact, it is not even loaded on subsequent imports), making it run more than once is not so difficult. A good example (not the only way to do so) is discussed here .
Your code will be re-executed when you reload your module with
reload, which may also be undesirable if that code has certain side effects.
This code can be executed for a long time, which will slow down the loading of the module and, as a result, the launch of the application.
At the same time, I want to note that in general this opportunity (in moderation) is used everywhere, and, again, in moderate quantities, in most cases it does not cause problems. For example, a function decorator is the execution of user code during module loading. Although, if a decorator doesn't just "change" the function it's decorating, but has side effects, this can (but by no means necessarily) be a source of problems if you don't understand all of these mechanisms.
On the second question, assigning values (and not the results of user function calls) to global variables, creating functions and classes, importing other modules, that is, in some way, declarative code (causing the execution of not your user logic, but the logic of the interpreter), in the context of this statement is not considered code execution.
In general, the moral is that you just need to clearly understand the possible consequences of executing the code when importing a module in Python, and use this opportunity wisely. The original statement about the inadmissibility of this is akin to saying that
goto is an absolute evil. Yes, thoughtless use is fraught with huge problems, but at the same time, in the same Linux kernel,
goto is used everywhere for those things where it is really appropriate.