我想问这是否是为类静态字段编写get / set函数的最佳方法,这是类实例的属性,而不是类本身?
{
"collectionName" : "Collection A",
"items" : [
{ "id" : "001", "itemName" : "itemName001", "itemDescription" : "Foo was here..." },
{ "id" : "002", "itemName" : "itemName002", "itemDescription" : "Foo was here..." },
]
}
这是一些输出:
class MyClass:
__static_field = "something"
def __init__(self):
pass
@staticmethod
def get_static_field():
return MyClass.__static_field
def get_static_field_with_instance(self):
return self.__class__.__static_field
def set_static_field_with_instance(self, new_value):
self.__class__.__static_field = new_value
答案 0 :(得分:1)
查看https://stackoverflow.com/a/7864317/2161778以获得直接答案。这是python的描述符协议的好转。
另外,看到那里接受的答案,这样的概念在python中没有意义99.9%的时间