說明:
Property 是 python 內的一個內置函數,主要可以回傳、設置、刪除 一個參數或物件的特質屬性。
而這個特點可以用來訪問具有私有屬性的變數或函數。
功能:
Property主要的功能如下,主要都是對屬性值的操作:
fget = fget function可以得到屬性值,
fset = fset function可以設定屬性值,
fdel = fdel function 可以刪除屬性值,
範例:
1.私有屬性外部無法直接存取
class TEST:
def __init__(self):
self.__x = 20
self.y = 10
def getx(self):
return self.__x
def setx(self, value):
self.__x = value
def delx(self):
del self.__x
c = TEST()
print(c.y)
print(c.__x)
'''
Compile result:
10.
Traceback (most recent call last):
File "####################/ex_property.py", line 18, in <module>
print(c.__x)
AttributeError: 'TEST' object has no attribute '__x'
'''
2.使用方法間接存取
但這樣的方式顯然太麻煩。
class TEST:
def __init__(self):
self.__x = 20
self.y = 10
def getx(self):
return self.__x
def setx(self, value):
self.__x = value
def delx(self):
del self.__x
c = TEST()
c.setx(20)
print(c.y)
print(c.getx())
'''
Compile result:
10
20
'''
3.使用property的方式訪問並修改
整體更值觀,也更方便使用,但程式碼本身卻還是有點冗長。
class TEST:
def __init__(self):
self.__x = 20
self.y = 10
def getx(self):
return self.__x
def setx(self, value):
self.__x = value
def delx(self):
del self.__x
x = property(getx,setx,delx)
c = TEST()
c.x = 20
print(c.y)
print(c.x)
'''
Compile result:
10
20
'''
3.使用decorator 加上 property簡化程式碼
class TEST:
def __init__(self):
self.__x = 20
self.y = 10
@property
def x(self):
return self.__x
@x.setter
def x(self, value):
self.__x = value
@x.deleter
def x(self):
del self.__x
c = TEST()
c.x = 20
print(c.y)
print(c.x)
del c.x
print(c.x)
'''
Compile result:
10
20
Traceback (most recent call last):
File "###\ex_property.py", line 24, in <module>
print(c.x)
File "###\ex_property.py", line 7, in x
return self.__x
AttributeError: 'TEST' object has no attribute '_TEST__x'
'''
補充:
Built-in Functions - Python 3.11.0 documentation
class property(fget=None, fset=None, fdel=None, doc=None)
Return a property attribute.
fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute.
A typical use is to define a managed attribute x
:
class C:
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
If c is an instance of C, c.x
will invoke the getter, c.x = value
will invoke the setter, and del c.x
the deleter.
If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget’s docstring (if it exists). This makes it possible to create read-only properties easily using [property()](https://docs.python.org/3/library/functions.html#property)
as a decorator:
**class** **Parrot**:
**def** __init__(self):
self._voltage = 100000
@property
**def** voltage(self):
*"""Get the current voltage."""***return** self._voltage
The @property
decorator turns the voltage()
method into a “getter” for a read-only attribute with the same name, and it sets the docstring for voltage to “Get the current voltage.”
A property object has getter
, setter
, and deleter
methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function. This is best explained with an example:
**class** **C**:
**def** __init__(self):
self._x = **None**@property
**def** x(self):
*"""I'm the 'x' property."""***return** self._x
@x.setter
**def** x(self, value):
self._x = value
@x.deleter
**def** x(self):
**del** self._x
This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (x
in this case.)
The returned property object also has the attributes fget
, fset
, and fdel
corresponding to the constructor arguments.
Changed in version 3.5: The docstrings of property objects are now writeable.