本篇文章帶給大家的內容是關於Python中@property裝飾器的技巧性用法(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
@property裝飾器能把一個方法變成屬性一樣來調用,下面我們就一起來看看Python的黑魔法@property裝飾器的使用技巧解析
@屬性有什麼用呢?表面看來,就是將一個方法用屬性的方式來存取。
上代碼
class Circle(object): def __init__(self, radius): self.radius = radius @property def area(self): return 3.14 * self.radius ** 2 c = Circle(4) print c.radius print c.area
可以看到,面積雖然是定義成一個方法的形式,但是加上@財產後,可以直接c.area,當成屬性訪問。
現在問題來了,每次呼叫c.area,都會計算一次,太浪費cpu了,怎麼才能只算一次呢?這就是懶惰的財產。
class lazy(object): def __init__(self, func): self.func = func def __get__(self, instance, cls): val = self.func(instance) setattr(instance, self.func.__name__, val) return val class Circle(object): def __init__(self, radius): self.radius = radius @lazy def area(self): print 'evalute' return 3.14 * self.radius ** 2 c = Circle(4) print c.radius print c.area print c.area print c.area
可以看到, 'evalute' 只輸出了一次,對@Lazy的機制應該很好理解。
在這裡,懶惰類別有__get__方法,表示是個描述器,第一次執行c.area的時候,因為順序問題,先去Ç.__ dict__中找,沒找到,就去類別空間找,在類別圈中,有面積()方法,於是就被__get__攔截。
在__get__中,呼叫實例的區域()方法算出結果,並動態為實例新增個同名屬性把結果賦給它,即加到Ç.__ dict__中。
再執行c.area的時候,先去Ç.__ dict__找,因為此時已經有了,就不會經過區域()方法和__get__了。
注意點
請注意以下程式碼場景:
程式碼片段1:
class Parrot(object): def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage if __name__ == "__main__": # instance p = Parrot() # similarly invoke "getter" via @property print p.voltage # update, similarly invoke "setter" p.voltage = 12
程式碼片段2:
class Parrot: def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage if __name__ == "__main__": # instance p = Parrot() # similarly invoke "getter" via @property print p.voltage # update, similarly invoke "setter" p.voltage = 12
程式碼1 ,2的差別在於
class Parrot(物件):
在python2下,分別執行測試
片段1:將會提示一個預期的錯誤訊息AttributeError:無法設定屬性
片段2:正確運行
參考python2文檔,@ property將提供一個ready-only屬性,以上程式碼沒有提供對應的@ voltage.setter,按理說片段2程式碼將提示運行錯誤,在python2文件中,我們可以找到以下資訊:
BIF:
property([fget [,fset [,fdel [,doc]]]])
傳回新樣式類別的屬性屬性(從物件派生的類別)。
原來在python2下,內建類型物件並不是預設的基類,如果在定義類別時,沒有明確說明的話(程式碼片段2),我們定義的Parrot(程式碼片段2)將不會繼承物件
而物件類別正好提供了我們需要的@property功能,在文件中我們可以查到如下資訊:
新式課程
任何繼承自object的類。這包括所有內建類型,如list和dict。只有新式類別可以使用Python的更新,通用的功能,如__slots__,描述符,屬性和__getattribute __()。
同時我們也可以透過以下方法來驗證
class A: pass >>type(A) <type></type>
class A(object): pass >>type(A) <type></type>
從傳回的
為了考慮程式碼的python版本過渡期的兼容性問題,我覺得應該定義類別文件的時候,都應該明確定義對象,做為一個好習慣
最後的程式碼將如下:
class Parrot(object): def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage @voltage.setter def voltage(self, new_value): self._voltage = new_value if __name__ == "__main__": # instance p = Parrot() # similarly invoke "getter" via @property print p.voltage # update, similarly invoke "setter" p.voltage = 12#
以上是Python中@property裝飾器的技巧用法(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。