I have put a Jira client for my django application together and now need to make it static but I can’t figure how to convert @property and @?.setter into static fields:
Say I have a class:
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)
and I want its methods to be static:
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)
can’t mark get_a as @property now as calling it will return an object and not actually call get_a. Methods are something I can live with but is there a way to have getters/setters instead?
The easiest way is to make this class singleton. Instead of making methods static, override class with its instance:
You can also use metaclasses if you want to have more than one instance.
http://en.wikibooks.org/wiki/Python_Programming/MetaClasses