The other answer you referenced is correct, but "all the code at indentation 0 is executed" does not mean "none of the code at other indentation levels is executed". In this case, processing the class
statements (which are at indentation 0) requires executing the indented code they contain.
Obviously, this line is executed and does what you expect:
print("Line 1")
This block at the end is executed, defining a function. The function never gets called, so this print statement is not actually executed
def functionA():
print("Running code in function")
The two class definitions are also executed. Executing a class definition creates a class object. In order to initialize that class object, the code within the class definition is executed. Most often, this would consist of def
statements to define functions within the class. Just like the free-standing def
example, the def
statement would be executed, creating a function object, but the function would not be called.
In your case, you have code in the class definitions that does stuff other than define functions. This allows you to have code that sets initial state for the class object. These are executed as part of processing the class
statement, and therefore you see their side effects at that point.