Static class members python

南笙酒味 提交于 2019-11-30 17:46:54

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:

class Foo:
    bar = 1

print Foo.bar # prints '1'

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:

# Create static member
class Foo:
    bar = 1

# Create and destroy object of type Foo
foo = Foo()
del foo

# Check that static members survive
print Foo.bar # Still prints '1'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!