Is there a static constructor or static initializer in Python?

后端 未结 6 755
星月不相逢
星月不相逢 2020-12-17 07:56

Is there such a thing as a static constructor in Python?

How do I implement a static constructor in Python?

Here is my code... The

6条回答
  •  情书的邮戳
    2020-12-17 08:29

    There's a fundamental difference between static and dynamic languages that isn't always apparent at first.

    In a static language, the class is defined at compile time and everything is all nice and set in concrete before the program ever runs.

    In a dynamic language, the class is actually defined at runtime. As soon as the interpreter parses and starts executing all of those classes and def statements, the equivalent of a static constructor is being run. The class definitions are being executed at that point.

    You can put any number of statements anywhere inside the class body and they are in effect a static constructor. If you want, you can place them all in a function that doesn't take self as a parameter, and call that function at the end of the class.

提交回复
热议问题