python:python_misc:properties_in_python_classes
This is an old revision of the document!
class ClickerCounter(): def __init__(self, upper): self.count = 0 # instance variable for storing the count self.__limit = upper # instance variable for storing the limit def click(self): """ Click the counter: advance the count by one and reset if over the upper limit. """ self.count = self.count + 1 # wrap the count if over the limit if self.count > self.__limit: self.count = 0 def reset(self): """Reset the counter.""" self.count = 0 @property def limit(self): return self.__limit @limit.setter def limit(self, l): """Set a new count limit""" if l > 0: self.__limit = l else: print('Invalid new limit:', l) c = ClickerCounter(100) c.click() c.click() print(c.count) print(c.limit) c.limit = 1000 print(c.limit) c.limit = 0
python/python_misc/properties_in_python_classes.1550617113.txt.gz · Last modified: 2019/02/19 22:58 by mithat