python中property
Ⅰ python的property函數有什麼用
可以直接在Emacs中打開Shell然後運行Python,也可以安裝ipython模式的插件,後者好一點,順便增強了原來的python模式的功能。
Ⅱ python 為什麼要用property
python的內置函數built-in function是指在標准庫standard library中的內置函數。這些函數不屬於任何類或模塊,可以直接調用,可以看成python語言不可分割的一部分吧。近似於關鍵字。
abs()
dict()
help()
min()
setattr()
all()
dir()
hex()
next()
slice()
any()
divmod()
id()
object()
sorted()
ascii()
enumerate()
input()
oct()
staticmethod()
bin()
eval()
int()
open()
str()
bool()
exec()
isinstance()
ord()
sum()
bytearray()
filter()
issubclass()
pow()
super()
bytes()
float()
iter()
print()
tuple()
callable()
format()
len()
property()
type()
chr()
frozenset()
list()
range()
vars()
classmethod()
getattr()
locals()
repr()
zip()
compile()
globals()
map()
reversed()
__import__()
complex()
hasattr()
max()
round()
delattr()
hash()
memoryview()
set()
標准庫中其他模塊內的靜態方法等不屬於內置函數。
用戶自定義擴充的函數也不屬於內置。
=============
但在其他語言、其他環境下,內置函數的具體含義不同。
內置函數在概念上並沒有唯一專指,是合成詞,最好理解為「被內置了的一些函數」
Ⅲ python property能不能多個參數
如下:
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
Ⅳ 如何理解python中的@property 知乎
@property 可以將python定義的函數「當做」屬性訪問,從而提供更加友好訪問方式,但是有時候setter/getter也是需要的!
Ⅳ python 使用property為什麼要定義私有
不是使用property才要私有的,而是為了調用私有方法方便,才使用property的,具體請看下面的詳解
在綁定屬性時,如果直接把屬性暴露出去,雖然寫起來很簡單,但是,沒辦法檢查參數,導致可以把成績隨便改:
s=Student()
s.score=9999
這顯然不合邏輯。為了限制score的范圍,可以通過一個set_score()方法來設置成績,再通過一個get_score()來獲取成績,這樣,在set_score()方法里,就可以檢查參數:
classStudent(object):
defget_score(self):
returnself._score
defset_score(self,value):
ifnotisinstance(value,int):
raiseValueError('scoremustbeaninteger!')
ifvalue<0orvalue>100:
raiseValueError('scoremustbetween0~100!')
self._score=value
現在,對任意的Student實例進行操作,就不能隨心所欲地設置score了:
>>>s=Student()
>>>s.set_score(60)#ok!
>>>s.get_score()
60
>>>s.set_score(9999)
Traceback(mostrecentcalllast):
...
ValueError:scoremustbetween0~100!
但是,上面的調用方法又略顯復雜,沒有直接用屬性這么直接簡單。
有沒有既能檢查參數,又可以用類似屬性這樣簡單的方式來訪問類的變數呢?對於追求完美的Python程序員來說,這是必須要做到的!
還記得裝飾器(decorator)可以給函數動態加上功能嗎?對於類的方法,裝飾器一樣起作用。Python內置的@property裝飾器就是負責把一個方法變成屬性調用的:
class Student(object):
@property
defscore(self):
returnself._score
@score.setter
defscore(self,value):
ifnotisinstance(value,int):
raiseValueError('scoremustbeaninteger!')
ifvalue<0orvalue>100:
raiseValueError('scoremustbetween0~100!')
self._score=value
@property的實現比較復雜,先考察如何使用。把一個getter方法變成屬性,只需要加上@property就可以了,此時,@property本身又創建了另一個裝飾器@score.setter,負責把一個setter方法變成屬性賦值,於是,就擁有一個可控的屬性操作:
>>>s=Student()
>>>s.score=60#OK,實際轉化為s.set_score(60)
>>>s.score#OK,實際轉化為s.get_score()
60
>>>s.score=9999
Traceback(mostrecentcalllast):
...
ValueError:scoremustbetween0~100!
注意到這個神奇的@property,在對實例屬性操作的時候,就知道該屬性很可能不是直接暴露的,而是通過getter和setter方法來實現的。
還可以定義只讀屬性,只定義getter方法,不定義setter方法就是一個只讀屬性:
classStudent(object):
@property
defbirth(self):
returnself._birth
@birth.setter
defbirth(self,value):
self._birth=value
@property
defage(self):
return2014-self._birth
上面的birth是可讀寫屬性,而age就是一個只讀屬性,因為age可以根據birth和當前時間計算出來。
一句話總結
@property廣泛應用在類的定義中,可以讓調用者寫出簡短的代碼,同時保證對參數進行必要的檢查,這樣,程序運行時就減少了出錯的可能性。
Ⅵ 關於Python的property怎麼理解呢 老是理解不了
這個,沒編程經驗的人確實難理解。。。
因為它涉及了兩個東西
1.面對對象的封裝
2.python的裝飾器思想
面向對象裡面,一般是只提供方法去操作對象的屬性,而不是直接操作。所以對任意一個熟悉,一般都有對於的getxx()和setxx()方法,意思是指獲取某個屬性的值和設置某個屬性的值。
裝飾器的意思是對原有函數,多做點事。它本質上也是一個函數,只不過輸入是函數,返回的也是函數,只是在中間會多做點其他事情,這里會涉及到一個閉包的概念。
所以python的property,就是簡化了用戶需要設置或者獲取某個屬性的時候,需要知道getxx()和setxx()之類的函數,直接把對象的某個屬性傻瓜式操作了,通過對象,直接對某個賦值,取值就行。
Ⅶ python中給類添加property之後怎麼給property添加__add__
你這里的a.attr 是 int對象,a.attr+ b.attr ,就是4+10 = (4,10),是要重寫 int的__add__方法。
或者,重寫A的方法
Ⅷ 請問python描述符property中的self.fget(instance)怎麼理解
self.fget等價於 __init__(self,fget=None,fset=None,fdel=None) 方法中的 fset參數
==================看這下面=================================
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 = MyProperty(getX, setX, delX)
執行上面的語句後self.fget就等於類C里的getX
self.fget(instance)等價於調用C里的getX(instance)方法
===========我是新手剛看到這如有興趣可共同學習================