@property
class Animal(object):
def __init__(self, name, age):
self._name = name
self._age = age
self._color = 'Black'
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if isinstance(value, basestring):
self._name = value
else:
self._name = 'No name'
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if value > 0 and value < 100:
self._age = value
else:
self._age = 0
# print 'invalid age value.'
@property
def color(self):
return self._color
@color.setter
def color(self, value):
self._color = value;
a = Animal('black dog', 3)
a.name = 'white dog'
a.age = 300
print 'Name:', a.name
print 'Age:', a.age
property函数
它以一个函数形式,定义一个属性,与@property实现原理类似,或者就是它的的变异用法。
其原型为:
property(fget=None, fset=None, fdel=None, doc=None)
-
class Animal(object): def __init__(self, name, age): self._name = name self._age = age self._color = 'Black' def get_name(self): return self._name def set_name(self, value): if isinstance(value, basestring): self._name = value else: self._name = 'No name' name = property(fget=get_name, fset=set_name, fdel=None, doc='name of an animal') def get_age(self): return self._age def set_age(self, value): if value > 0 and value < 100: self._age = value else: self._age = 0 # print 'invalid age value.' age = property(fget=get_age, fset=set_age, fdel=None, doc='name of an animal') a = Animal('black dog', 3) a.name = 'white dog' a.age = 3 print 'Name:', a.name print Animal.name.__doc__ print 'Age:', a.age