Python運算符重載
????? Python語言提供了運算符重載功能,增強了語言的靈活性,這一點與C++有點類似又有些不同。鑒于它的特殊性,今天就來討論一下Python運算符重載。
????? Python語言本身提供了很多魔法方法,它的運算符重載就是通過重寫這些Python內置魔法方法實現的。這些魔法方法都是以雙下劃線開頭和結尾的,類似于__X__的形式,python通過這種特殊的命名方式來攔截操作符,以實現重載。當Python的內置操作運用于類對象時,Python會去搜索并調用對象中指定的方法完成操作。
?????? 類可以重載加減運算、打印、函數調用、索引等內置運算,運算符重載使我們的對象的行為與內置對象的一樣。Python在調用操作符時會自動調用這樣的方法,例如,如果類實現了__add__方法,當類的對象出現在+運算符中時會調用這個方法。
常見運算符重載方法
方法名 |
重載說明 |
運算符調用方式 |
__init__ |
構造函數 |
對象創建: X = Class(args) |
__del__ |
析構函數 |
X對象收回 |
__add__/__sub__ |
加減運算 |
?X+Y, X+=Y/X-Y, X-=Y |
__or__ |
運算符| |
X|Y, X|=Y |
_repr__/__str__ |
打印/轉換 |
print(X)、repr(X)/str(X) |
__call__ |
函數調用 |
X(*args, **kwargs) |
__getattr__ |
屬性引用 |
X.undefined |
__setattr__ |
屬性賦值 |
X.any=value |
__delattr__ |
屬性刪除 |
del X.any |
__getattribute__ |
屬性獲取 |
X.any |
__getitem__ |
索引運算 |
X[key],X[i:j] |
__setitem__ |
索引賦值 |
X[key],X[i:j]=sequence |
__delitem__ |
索引和分片刪除 |
del X[key],del X[i:j] |
__len__ |
長度 |
len(X) |
__bool__ |
布爾測試 |
bool(X) |
__lt__, __gt__,? __le__, __ge__,? __eq__, __ne__ |
特定的比較 |
依次為X
X==Y,X!=Y? 注釋:(lt: less than, gt: greater than,? ? le: less equal, ge: greater equal,? ? eq: equal, ne: not equal? ) |
__radd__ |
右側加法 |
other+X |
__iadd__ |
實地(增強的)加法 |
X+=Y(or else __add__) |
__iter__, __next__ |
迭代 |
I=iter(X), next() |
__contains__ |
成員關系測試 |
item in X(X為任何可迭代對象) |
__index__ |
整數值 |
hex(X), bin(X),? oct(X) |
__enter__, __exit__ |
環境管理器 |
with obj as var: |
__get__, __set__,? __delete__ |
描述符屬性 |
X.attr, X.attr=value, del X.attr |
__new__ |
創建 |
在__init__之前創建對象 |
?? 下面對常用的運算符方法的使用進行一下介紹。
構造函數和析構函數:__init__和__del__
?????? 它們的主要作用是進行對象的創建和回收,當實例創建時,就會調用__init__構造方法。當實例對象被收回時,析構函數__del__會自動執行。
>>> class Human(): ... def __init__(self, n): ... self.name = n ... print("__init__ ",self.name) ... def __del__(self): ... print("__del__") ... >>> h = Human('Tim') __init__ Tim >>> h = 'a' __del__
加減運算:__add__和__sub__
?????? 重載這兩個方法就可以在普通的對象上添加+-運算符操作。下面的代碼演示了如何使用+-運算符,如果將代碼中的__sub__方法去掉,再調用減號運算符就會出錯。
>>> class Computation(): ... def __init__(self,value): ... self.value = value ... def __add__(self,other): ... return self.value + other ... def __sub__(self,other): ... return self.value - other ... >>> c = Computation(5) >>> c + 5 10 >>> c - 3 2
對象的字符串表達形式:__repr__和__str__
?????? 這兩個方法都是用來表示對象的字符串表達形式:print()、str()方法會調用到__str__方法,print()、str()和repr()方法會調用__repr__方法。從下面的例子可以看出,當兩個方法同時定義時,Python會優先搜索并調用__str__方法。
>>> class Str(object): ... def __str__(self): ... return "__str__ called" ... def __repr__(self): ... return "__repr__ called" ... >>> s = Str() >>> print(s) __str__ called >>> repr(s) '__repr__ called' >>> str(s) '__str__ called'
索引取值和賦值:__getitem__, __setitem__
?????? 通過實現這兩個方法,可以通過諸如 X[i] 的形式對對象進行取值和賦值,還可以對對象使用切片操作。
>>> class Indexer: data = [1,2,3,4,5,6] def __getitem__(self,index): return self.data[index] def __setitem__(self,k,v): self.data[k] = v print(self.data) >>> i = Indexer() >>> i[0] 1 >>> i[1:4] [2, 3, 4] >>> i[0]=10 [10, 2, 3, 4, 5, 6]
設置和訪問屬性:__getattr__、__setattr__
?????? 我們可以通過重載__getattr__和__setattr__來攔截對對象成員的訪問。__getattr__在訪問對象中不存在的成員時會自動調用。__setattr__方法用于在初始化對象成員的時候調用,即在設置__dict__的item時就會調用__setattr__方法。具體例子如下:
class A(): def __init__(self,ax,bx): self.a = ax self.b = bx def f(self): print (self.__dict__) def __getattr__(self,name): print ("__getattr__") def __setattr__(self,name,value): print ("__setattr__") self.__dict__[name] = value a = A(1,2) a.f() a.x a.x = 3 a.f()
???? 上面代碼的運行結果如下,從結果可以看出,訪問不存在的變量x時會調用__getattr__方法;當__init__被調用的時候,賦值運算也會調用__setattr__方法。
__setattr__ __setattr__ {'a': 1, 'b': 2} __getattr__ __setattr__ {'a': 1, 'x': 3, 'b': 2}
迭代器對象: __iter__,? __next__
?????? Python中的迭代,可以直接通過重載__getitem__方法來實現,看下面的例子。
>>> class Indexer: ... data = [1,2,3,4,5,6] ... def __getitem__(self,index): ... return self.data[index] ... >>> x = Indexer() >>> for item in x: ... print(item) ... 1 2 3 4 5 6
????? 通過上面的方法是可以實現迭代,但并不是最好的方式。Python的迭代操作會優先嘗試調用__iter__方法,再嘗試__getitem__。迭代環境是通過iter去嘗試尋找__iter__方法來實現,而這種方法返回一個迭代器對象。如果這個方法已經提供,Python會重復調用迭代器對象的next()方法,直到發生StopIteration異常。如果沒有找到__iter__,Python才會嘗試使用__getitem__機制。下面看一下迭代器的例子。
class Next(object): def __init__(self, data=1): self.data = data def __iter__(self): return self def __next__(self): print("__next__ called") if self.data > 5: raise StopIteration else: self.data += 1 return self.data for i in Next(3): print(i) print("-----------") n = Next(3) i = iter(n) while True: try: print(next(i)) except Exception as e: break
?? 程序的運行結果如下:
__next__ called 4 __next__ called 5 __next__ called 6 __next__ called ----------- __next__ called 4 __next__ called 5 __next__ called 6 __next__ called
??? 可見實現了__iter__和__next__方法后,可以通過for in的方式迭代遍歷對象,也可以通過iter()和next()方法迭代遍歷對象。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
