Advertisement
furas

Python - getter, setter

May 2nd, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Animal:
  2.  
  3.     def __init__(self, **kwargs):
  4.         self.properties = kwargs
  5.  
  6.     @property
  7.     def color(self):
  8.         print('DEBUG: color getter')
  9.         return self.properties
  10.  
  11.     @color.setter
  12.     def color(self, value):
  13.         print('DEBUG: color setter')
  14.         self.properties['color'] = value
  15.  
  16. dog = Animal()
  17.  
  18. print("DEBUG: dog.color['new_dog'] = 'Brown'")
  19. dog.color['new_dog'] = 'Brown'
  20.  
  21. print("DEBUG: dog.color = 'Red'")
  22. dog.color = 'Red'
  23.  
  24. print("DEBUG: print(dog.color)")
  25. print(dog.color)
  26.  
  27. cat = Animal()
  28.  
  29. print("DEBUG: cat.color = 'white'")
  30. cat.color = 'white'
  31.  
  32. print("DEBUG: print(cat.color)")
  33. print(cat.color)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement