静态类成员 python
因此,我使用静态类成员,这样我就可以在类方法和同一类的静态方法之间共享数据(该类只有 1 个实例化)。我明白这一点,但我只是想知道静态成员何时初始化?是进口的吗?第一次使用该类时?因为我将从超过 1 个模块(因此超过 1 个 import 语句)调用此类的静态成员。所有访问静态方法的模块都会共享相同的静态数据成员吗?如果我的主要客户端删除了我的类的实例,然后重新创建它(不完全终止或重新导入内容),我的数据成员会被保留吗?
So I'm using static class members so I can share data between class methods and static methods of the same class (there will only be 1 instantiation of the class). I understand this fine, but I'm just wondering when the static members get initialized? Is it on import? On the first use of the class? Because I'm going to be calling the static members of this class from more than 1 module (therefore more than 1 import statement). Will all the modules accessing the static methods share the same static data members? And if my main client deletes the instance of my class, and then recreates it (without terminating altogether or re-importing stuff), will my data members be preserved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们将在类定义时初始化,如果您将类作为模块的一部分导入,这将在导入时发生。假设“静态”类成员定义样式如下:
请注意,这是静态类成员,无需实例化该类。
import 语句将只执行一次模块的内容,无论执行多少次或在何处执行。
是的,静态成员将被任何访问它们的代码共享。
是的,如果删除类型为该类的对象,该类的静态成员将被保留:
They will be initialized at class definition time, which will happen at import time if you are importing the class as part of a module. This assuming a "static" class member definition style like this:
Note that, this being a static class member, there is no need to instantiate the class.
The import statement will execute the contents of a module exactly once, no matter how many times or where it is executed.
Yes, the static members will be shared by any code accessing them.
Yes, the static members of a class will be preserved if you delete an object whose type is that class: