User Tools

Site Tools


python:python_misc:properties_in_python_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both 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,​ upper):     def __init__(self,​ upper):
-        self.count = 0       ​# instance variable for storing the count+        self.__count ​= 0     ​# instance variable for storing the 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 ​+ 1+        self.__count ​+1
         # wrap the count if over the limit         # wrap the count if over the limit
-        if self.count > self.__limit: ​  +        if self.__count ​> self.__limit: ​  
-            self.count = 0+            self.__count ​= 0
    
     def reset(self):​     def reset(self):​
         """​Reset the counter."""​         """​Reset the counter."""​
-        self.count = 0+        self.__count ​= 0 
 +  
 +    @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('​Invalid new limit:',​ l)             print('​Invalid new limit:',​ l)
 + 
 c = ClickerCounter(100) c = ClickerCounter(100)
 + 
 c.click() c.click()
 c.click() c.click()
-print(c.count) +print(c.count) ​ # 2 
-print(c.limit) +print(c.limit) ​ # 100 
 + 
 c.limit = 1000 c.limit = 1000
-print(c.limit) +print(c.limit) ​ # 1000 
 + 
 c.limit = 0 c.limit = 0
 +print(c.limit) ​ # 1000
 </​code>​ </​code>​
python/python_misc/properties_in_python_classes.txt · Last modified: 2019/06/21 23:16 by mithat