直奔主題
使用中文注釋需要使用
#-*-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,不能為其他的。詳情看例子)
class Demo(object): def __init__(self): print("這是構造函數") self._name = None @property def name(self): return self._name #這是屬性get的方法 @name.setter def name(self, value):#這是屬性set的方法 self._name = value @name.deleter def name(self): print("this is the method of delete") self._name = None if __name__ == "__main__": testDemo = Demo() print(testDemo.name) testDemo.name = "name" print(testDemo.name) del testDemo.name
以下是結果:
#======以下是結果======== 這是構造函數 None name this is the method of delete
2.使用property()方法 property(fget,fset,fdel,doc)
要求:(1)所用的類為新式類,在python3版本以上默認為新式類,或者是是直接或間接繼承object的類
class Demo2(object): def __init__(self): print("這是構造函數") self._value = None def getValue(self):#這是屬性value的get方法 print("getting the value of property") return self._value def setValue(self, value):#這是屬性value的set方法 print("setting the value of property") self._value = value def 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') this is the method of delete
使用屬性property2種方法存放多個值的例子
1.使用裝飾器@property進行設置 屬性含有多個值
class Demo4(object): def __init__(self): print("這是構造函數") self._value1 = None self._value2 = None @property def values(self): return self._value1, self._value2 #這是屬性get的方法 @values.setter def values(self, values):#這是屬性set的方法 self._value1, self._value2 = values @values.deleter def values(self): print("this is the method of delete") self._value1 = None self._value2 = None if __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')) this is the method of delete
2.property(fget, fset, fdel, doc)方法設置 屬性含有多個值
class Demo3(object): def __init__(self): print("這是構造函數") self._value = None self._value2 = None def getoValue(self): # 這是屬性value的get方法 print("getting the value of property") return self._value, self._value2 def setoValue(self, values): # 這是屬性value的set方法 print("setting the value of property") self._value, self._value2 = values def 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')) this is 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 value is not exist!