Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- @version: v1.0
- @author: shansc
- @license: Apache Licence
- @contact: shansc@taou.com
- @time: 2018/4/14 上午10:28
- """
- class Student(object):
- @property
- def birth(self):
- return self._birth
- @birth.setter
- def birth(self, value):
- self._birth = value
- @property
- def age(self):
- return 2014 - self._birth
- class Student1(object):
- def _get_birth(self):
- return self._birth
- def _set_birth(self, value):
- self._birth = value
- birth = property(fget=_get_birth, fset=_set_birth, doc='birth setter and getter')
- del _get_birth, _set_birth
- def _get_age(self):
- return 2014 - self._birth
- age = property(fget=_get_age, doc='age getter')
- del _get_age
- student = Student()
- student.birth = 10
- print student.birth
- # student.age = 50 # AttributeError: can't set attribute
- print student.age
- print '***' * 10
- student1 = Student1()
- student1.birth = 10
- print student1.birth
- student.age = 50 # AttributeError: can't set attribute
- print student1.age
Add Comment
Please, Sign In to add comment