我已经为我的 django 应用程序放置了一个 Jira 客户端,现在需要将其设为静态,但我不知道如何将其转换@property
为@?.setter
静态字段:
假设我有一堂课:
class nothing(object):
auth_token = None
counter = 0
@property
def a(self):
self.log('getter')
if not self.auth_token:
self.auth_token = 'default'
return self.auth_token
@a.setter
def a(self, value):
self.log('setter')
self.auth_token = value
def log(self, value):
self.counter+=1
print '{0} called / counter: {1}'.format(value, self.counter)
我希望它的方法是静态的:
class nothing:
auth_token = None
counter = 0
@staticmethod
def get_a():
nothing.log('getter')
if not nothing.auth_token:
nothing.log('auth_token value is None, setting')
nothing.auth_token = 'default'
return nothing.auth_token
@staticmethod
def set_a(value):
nothing.log('setter')
nothing.auth_token = value
@staticmethod
def log(value):
nothing.counter+=1
print '{0} called / counter: {1}'.format(value, nothing.counter)
不能标记get_a
为@property
now 因为调用它将返回一个对象而不是实际调用get_a
. 方法是我可以忍受的东西,但是有没有办法让 getter/setter 代替?