python:python_misc:properties_in_python_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Last revisionBoth sides next revision | ||
python:python_misc:properties_in_python_classes [2019/02/19 23:12] – mithat | python:python_misc:properties_in_python_classes [2019/02/19 23:16] – mithat | ||
---|---|---|---|
Line 6: | Line 6: | ||
class ClickerCounter(): | class ClickerCounter(): | ||
def __init__(self, | def __init__(self, | ||
- | self.count = 0 | + | self.__count |
self.__limit = upper # instance variable for storing the limit | self.__limit = upper # instance variable for storing the limit | ||
Line 13: | Line 13: | ||
Advance the count by one and reset if over the upper limit. | Advance the count by one and reset if over the upper limit. | ||
""" | """ | ||
- | self.count = self.count | + | self.__count |
# wrap the count if over the limit | # wrap the count if over the limit | ||
- | if self.count > self.__limit: | + | if self.__count |
- | self.count = 0 | + | self.__count |
def reset(self): | def reset(self): | ||
""" | """ | ||
- | self.count = 0 | + | self.__count |
+ | |||
+ | @property | ||
+ | def count(self): | ||
+ | return self.__count | ||
@property | @property | ||
def limit(self): | def limit(self): | ||
return self.__limit | return self.__limit | ||
+ | |||
@limit.setter | @limit.setter | ||
def limit(self, l): | def limit(self, l): | ||
Line 33: | Line 37: | ||
else: | else: | ||
print(' | print(' | ||
+ | |||
c = ClickerCounter(100) | c = ClickerCounter(100) | ||
+ | |||
c.click() | c.click() | ||
c.click() | c.click() | ||
- | print(c.count) | + | print(c.count) |
- | print(c.limit) | + | print(c.limit) |
+ | |||
c.limit = 1000 | c.limit = 1000 | ||
- | print(c.limit) | + | print(c.limit) |
+ | |||
c.limit = 0 | c.limit = 0 | ||
+ | print(c.limit) | ||
</ | </ |
python/python_misc/properties_in_python_classes.txt · Last modified: 2019/06/21 23:16 by mithat