Guest User

Untitled

a guest
Apr 21st, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. @version: v1.0
  6. @author: shansc
  7. @license: Apache Licence
  8. @contact: shansc@taou.com
  9. @time: 2018/4/14 上午10:28
  10. """
  11.  
  12.  
  13. class Student(object):
  14.  
  15. @property
  16. def birth(self):
  17. return self._birth
  18.  
  19. @birth.setter
  20. def birth(self, value):
  21. self._birth = value
  22.  
  23. @property
  24. def age(self):
  25. return 2014 - self._birth
  26.  
  27.  
  28. class Student1(object):
  29. def _get_birth(self):
  30. return self._birth
  31.  
  32. def _set_birth(self, value):
  33. self._birth = value
  34.  
  35. birth = property(fget=_get_birth, fset=_set_birth, doc='birth setter and getter')
  36. del _get_birth, _set_birth
  37.  
  38. def _get_age(self):
  39. return 2014 - self._birth
  40.  
  41. age = property(fget=_get_age, doc='age getter')
  42. del _get_age
  43.  
  44.  
  45. student = Student()
  46. student.birth = 10
  47. print student.birth
  48. # student.age = 50 # AttributeError: can't set attribute
  49. print student.age
  50.  
  51. print '***' * 10
  52.  
  53. student1 = Student1()
  54. student1.birth = 10
  55. print student1.birth
  56. student.age = 50 # AttributeError: can't set attribute
  57. print student1.age
Add Comment
Please, Sign In to add comment