我已經把吉拉客戶對我的Django應用程序一起,現在需要讓靜態的,但我想不出如何@property
和@?.setter
轉換成靜態字段:Python靜態setter和getters?
說我有一個類:
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
現在和調用它會返回一個對象,而不是ACTU盟友電話get_a
。方法是我可以忍受的,但是有沒有辦法讓getter/setter改爲?