class Example:
name = "Example" # usually called a class-variable
@staticmethod
def static(newName=None):
if newName is not None:
Example.name = newName
print ("%s static() called" % Example.name)
@classmethod
def cls_static(cls, newName=None):
if newName is not None:
cls.name = newName
print ("%s static() called" % cls.name)
Example.static()
Example.static("john")
Example.cls_static()
Example.cls_static("bob")
当然,您应该如您所指出的那样创建一个静态(或类)变量。你知道吗
根据您的喜好,您可以使用其中一种。我让您阅读此链接以获取更多信息:http://radek.io/2011/07/21/static-variables-and-methods-in-python/
相关问题 更多 >
编程相关推荐