python基礎之類的property用法

在類中,經常會用以下方法來定義一個類,例:

>>> class student(object):
	def __init__(self):
		self.__englishScore=0
	def getScore(self):               #獲取屬性值的方法
		return self.__englishScore
	def setScore(self,value):         #設置屬性值的方法
		if value>=0 and value<=100:
			self.__englishScore=value
		else:
			print("輸入有誤,重新輸入")			
>>> a=student()
>>> a.setScore(83) #通過setScore方法設置屬性值
>>> a.getScore()   #通過getScore 方法獲取屬性值
83

以上代碼,創建了一個對象a,對象a的屬性賦值,訪問要通過setScore( )和getScore( )方法訪問,有點麻煩,能不能直接用

a.score=83,print(a.socre)?可以,因爲Python支持高階函數,可以用裝飾器函數把 get/set 方法“裝飾”成屬性調用:

>>> class student(object):
	def __init__(self):
		self.__englishScore=0
	@property       #裝飾get方法
	def englishScore(self):
		return self.__englishScore
	@englishScore.setter   #裝飾set方法
	def englishScore(self,value):
		if value>=0 and value<=100:
			self.__englishScore=value
		else:
			print("輸入有誤,重新輸入")		
>>> a=student()
>>> a.englishScore=83
>>> a.englishScore
83

注意: 第一個englishScore(self)是get方法,用@property裝飾,第二個englishScore(self, score)是set方法,用@englishScore.setter裝飾,@englishScore.setter是前一個@property裝飾後的副產品。

注意:以下紅色部分是裝飾後的屬性名稱,如果將“englishScore"改成"ss",在創建對象後就要用a.ss=83了。

class student(object):
    def __init__(self):
        self.__englishScore=0
    @property
    def englishScore(self):
        return self.__englishScore
    @englishScore.setter
    def englishScore(self,value):
        if value>=0 and value<=100:
            self.__englishScore=value
        else:
            print("輸入有誤,重新輸入")

一句話總結:@property將get和set方法裝飾成屬性,對象直接調用裝飾後的屬性,不必再調用get和set方法。

利用@property爲屬性設置只讀。例:

>>> class student(object):
	def __init__(self):
		self.__englishScore=0
		self.__mathScore=0
#-----------------------------------englishScore-----------------------------------------
	@property
	def englishScore(self):
		return self.__englishScore
	@englishScore.setter
	def englishScore(self,value):
		self.__englishScore=value
#-----------------------------------mathScore-----------------------------------------
	@property
	def mathScore(self):
		return self.__mathScore
	@mathScore.setter
	def mathScore(self,value):
		self.__mathScore=value
#-----------------------------------core-----------------------------------------
	@property
	def score(self):
		return self.__englishScore+self.__mathScore

>>> a=student()
>>> a.englishScore=78
>>> a.mathScore=67
>>> a.englishScore
78
>>> a.mathScore
67
>>> a.score
145

property在python3中的另外一種用法:

>>> class student(object):
	__slots__=["__englishScore"]     #限定屬性,用來證明此種方法的有效
	def __init__(self):
		self.__englishScore=0
	def getScore(self):              #設置get方法
		return self.__englishScore
	def setScore(self,value):        #設置set方法
		self.__englishScore=value
#-----------------------------------------property的使用-------------------------------------
	englishScore=property(getScore,setScore)  #使用property
#-----------------------------------------property的使用-------------------------------------
>>> a=student()
>>> a.englishScore=87
>>> a.englishScore
87
>>> a.math=93  #不允許添加變量,證明上面的englishScore是裝飾器裝飾出來的,不是動態添加的
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    a.math=93
AttributeError: 'student' object has no attribute 'math'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章