python(@property,setter,deleter)
1269 ワード
class Women(object):
def __init__(self, age):
# age , _age
self.age = age
def set_age(self,value):
if not isinstance(value,int):
raise TypeError('age must be a int value !')
self._age = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
# ,
if not isinstance(value, int):
raise TypeError('age must be a int value !')
self._age = value
#
if self._age >= 18:
# ,
self.is_ok = True
print(' ')
else:
self.is_ok = False
print(' !')
# @age.deleter age
@age.deleter
def age(self):
# ,
if hasattr(self, '_age'):
del self._age
print(' _age !')
else:
print('%s _age , '%self)
w = Women(0)
print(w.age,w.is_ok)
w.age = 18
print(w.age,w.is_ok)
del w.age
: _age !
del w.age
:
<__main__.women object="" at=""> _age ,