直奔主题
使用中文注释需要使用
#-*-coding:utf-8-*-
property
property在python中有2中使用property方法:
1.@property @属性名称.setter @属性名称.deleter
2.使用property(fget, fset, fdel, doc)指定
1.使用装饰器@property
要求:(1)所用的类为新式类,在python3版本以上默认为新式类,或者是是直接或间接继承object的类
(2)定义的函数名称必须要是名称名(如属性名是name,那函数名称就为name,不能为其他的。详情看例子)
classDemo(object):def __init__(self):print("这是构造函数")
self._name=None
@propertydefname(self):return self._name #这是属性get的方法
@name.setterdef name(self, value):#这是属性set的方法
self._name =value
@name.deleterdefname(self):print("this is the method of delete")
self._name=Noneif __name__ == "__main__":
testDemo=Demo()print(testDemo.name)
testDemo.name= "name"
print(testDemo.name)del testDemo.name
以下是结果:
#======以下是结果========
这是构造函数
None
name
thisis the method of delete
2.使用property()方法 property(fget,fset,fdel,doc)
要求:(1)所用的类为新式类,在python3版本以上默认为新式类,或者是是直接或间接继承object的类
classDemo2(object):def __init__(self):print("这是构造函数")
self._value=Nonedef getValue(self):#这是属性value的get方法
print("getting the value of property")returnself._valuedef setValue(self, value):#这是属性value的set方法
print("setting the value of property")
self._value=valuedef delValue(self):#这是属性value删除方法
print("this is the method of delete")
self._value=None
value= property(fget=getValue, fset=setValue, fdel=delValue, doc="this is a message about the property of value")if __name__ == "__main__":
testDemo=Demo2()print(testDemo.value)
testDemo.value= "name"
print("print the value of property:",testDemo.value)del testDemo.value
以下是结果:
#======以下是结果========
这是构造函数
getting the value of property
None
setting the value of property
getting the value of property
('print the value of property:', 'name')
thisis the method of delete
使用属性property2种方法存放多个值的例子
1.使用装饰器@property进行设置 属性含有多个值
classDemo4(object):def __init__(self):print("这是构造函数")
self._value1=None
self._value2=None
@propertydefvalues(self):return self._value1, self._value2 #这是属性get的方法
@values.setterdef values(self, values):#这是属性set的方法
self._value1, self._value2 =values
@values.deleterdefvalues(self):print("this is the method of delete")
self._value1=None
self._value2=Noneif __name__ == "__main__":
testDemo=Demo4()print(testDemo.values)
testDemo.values= "name","helloworld"
print("print the value of property:", testDemo.values)del testDemo.values
以下是结果:
#======以下是结果========
这是构造函数
(None, None)
('print the value of property:', ('name', 'helloworld'))
thisis the method of delete
2.property(fget, fset, fdel, doc)方法设置 属性含有多个值
classDemo3(object):def __init__(self):print("这是构造函数")
self._value=None
self._value2=Nonedef getoValue(self): #这是属性value的get方法
print("getting the value of property")returnself._value, self._value2def setoValue(self, values): #这是属性value的set方法
print("setting the value of property")
self._value, self._value2=valuesdef deloValue(self): #这是属性value删除方法
print("this is the method of delete")
self._value=None
values= property(fget=getoValue, fset=setoValue, fdel=deloValue, doc="this is a message about the property of values")if __name__ == "__main__":
testDemo=Demo3()print(testDemo.values)
testDemo.values= "name","helloworld"
print("print the value of property:", testDemo.values)del testDemo.values
以下是结果:
这是构造函数
getting the value of property
(None, None)
setting the value of property
getting the value of property
('print the value of property:', ('name', 'helloworld'))
thisis the method of delete
getter和setter
getter和setter 是在对象上动态的增加属性
if __name__ == "__main__":
testDemo=Demo2()print([n for n in dir(testDemo) if n[0] is not "_"])
setattr(testDemo,"newMethod", "hellworld")print([n for n in dir(testDemo) if n[0] is not "_"])
value= getattr(testDemo, "newMethod")print(value)#使用getattr获取一个不存在的属性,如果属性不存在,则返回一个默认的值
value = getattr(testDemo, "yoyo", "the value is not exist!")print(value)
以下是结果:
#======以下是结果========
这是构造函数
['delValue', 'getValue', 'setValue', 'value']
['delValue', 'getValue', 'newMethod', 'setValue', 'value']
hellworld
the valueis not exist!