總結Python常用的魔法方法
更新時間:2021年05月25日 16:46:17 作者:簡單生活,簡單愛
今天帶大家學習Python的相關知識,文中對Python常用的魔法方法作了非常詳細的總結,對正在學習python的小伙伴們有很好地幫助,需要的朋友可以參考下
一、算數(shù)運算符的魔法方法
- python2.2以后,對類和類型進行了統(tǒng)一,做法就是講int()、float()、str()、list()、tuple()這些BIF轉換為工廠函數(shù)(類對象)
- 給出以下算數(shù)運算符對應的魔法方法,前面和后面都被雙下劃線包尾說明是魔法方法
運算符 | 對應的魔法方法 | 中文注釋 |
+ | __ add__(self, other) | 加法 |
- | __ sub__(self, other) | 減法 |
* | __ mul__(self, other) | 乘法 |
/ | __ truediv__(self, other) | 真除法 |
// | __ floordiv__(self, other) | 整數(shù)除法 |
% | __ mod__(self, other) | 取余除法 |
divmod(a, b) | __ divmod__(self, other) | 把除數(shù)和余數(shù)運算結果結合,divmod(a,b)返回值是一個元組(a//b, a%b) |
** | __ pow__(self, other[,modulo]) | self的other次方再對modulo取余 |
<< | __ lshift__(self, other) | 按位左移 |
>> | __ rshift__(self, other) | 按位右移 |
& | __ and__(self, other) | 按位與操作 |
^ | __ xor__(self, other) | 按位異或操作(同為0,異為1) |
丨 | __ or__(self, other) | 按位或操作(有1則1) |
– | – | – |
- eg:
>>> type(len) <class 'builtin_function_or_method'> #普通的BIF >>> type(int) <class 'type'> #工廠函數(shù)(類對象),當調用它們的時候,其實就是創(chuàng)建了一個相應的實例對象 >>> type(dir) <class 'builtin_function_or_method'> >>> type(list) <class 'type'> >>> a = int('123') #創(chuàng)建一個相應的實例對象a >>> b = int('345') >>> a + b #python在兩個對象進行相加操作 468
- eg:舉個例子,下面定義一個比較特立獨行的類:
繼承int,并重寫__add__方法
>>> class New_int(int): def __add__(self,other): return int.__sub__(self,other) def __sub__(self,other): return int.__add__(self,other) >>> a = New_int(3) >>> b = New_int(5) >>> a + b #兩個對象相加,觸發(fā) __add__(self,other)方法 -2 >>> a - b 8 >>> 實例2:錯誤寫法,會造成無限遞歸 >>> class New_int(int): def __add__(self,other): return (self + other) def __sub__(self,other): return (self - other) >>> class New_int(int): def __add__(self,other): return (int(self) + int(other)) #將self與other強制轉換為整型,所以不會出現(xiàn)兩個對象相加觸發(fā)__add__()方法 def __sub__(self,other): return (int(self) - int(other)) >>> a = New_int(3) >>> b = New_int(5) >>> a + b 8
二、反運算相關的魔法方法
- 反運算相關的魔法方法
魔法方法 | 定義 |
__ radd__(self, other) | 定義加法的行為:+(當左操作數(shù)不支持相應的操作時被調用) |
__ rsub__(self, other) | 定義減法的行為:-(當左操作數(shù)不支持相應的操作時被調用) |
__ rmul__(self, other) | 定義乘法的行為:*(當左操作數(shù)不支持相應的操作時被調用) |
__ rtruediv__(self, other) | 定義真除法的行為:/(當左操作數(shù)不支持相應的操作時被調用) |
__ rfloordiv__(self, other) | 定義整數(shù)除法的行為://(當左操作數(shù)不支持相應的操作時被調用) |
__ rmod__(self, other) | 定義取模算法的行為:%(當左操作數(shù)不支持相應的操作時被調用) |
__ rdivmod__(self, other) | 定義當被divmod()調用時的行為(當左操作數(shù)不支持相應的操作時被調用) |
__ rpow__(self, other) | 定義當被power()調用或**運算時的行為(當左操作數(shù)不支持相應的操作時被調用) |
__ rlshift__(self, other) | 定義按位左移位的行為:<<(當左操作數(shù)不支持相應的操作時被調用) |
__ rrshift__(self, other) | 定義按位右移位的行為:>>(當左操作數(shù)不支持相應的操作時被調用) |
__ rand__(self, other) | 定義按位與操作的行為:&(當左操作數(shù)不支持相應的操作時被調用) |
__ rxor__(self, other) | 定義按位異或操作的行為:^(當左操作數(shù)不支持相應的操作時被調用) |
__ ror__(self, other) | 定義按位或操作的行為:丨(當左操作數(shù)不支持相應的操作時被調用) |
– | – |
>>> class int(int): def __add__(self,other): return int.__sub__(self,other) >>> a = int(3) >>> b = int(2) >>> a + b 1 反運算與算術運算符的不同之處是,反運算多了一個'r',例如 __add__()的反運算對應為 __radd__() >>> a + b 這里a是加數(shù),b是被加數(shù),如果a對象的__add__()方法沒有實現(xiàn)或者不支持相應的操作,那么python就會自動調用b的__radd__()方法 實例: >>> class Nint(int): def __radd__(self,other): return int.__sub__(self,other) >>> a = Nint(5) >>> b = Nint(3) >>> a + b #由于a對象默認有__add__()方法,所以b的__radd__()沒有執(zhí)行 8 實例2: >>> class Nint(int): def __radd__(self,other): return int.__sub__(self,other) >>> b = Nint(5) >>> 3 + b #由于3無__add__()方法,所以執(zhí)行b的反運算__radd__(self,other)方法,其中self是b對象 2
eg:注:在重寫反運算魔法方法時,一定要注意順序問題。得到的應該是個負數(shù),所以順序改變下。
三、增量賦值運算
增量賦值運算的魔法方法
魔法方法 | 定義 |
__ iadd__(self, other) | 定義賦值加法的行為:+= |
__ isub__(self, other) | 定義賦值減法的行為:-= |
__ imul__(self, other) | 定義賦值乘法的行為:*= |
__ itruediv__(self, other) | 定義賦值真除法的行為:/= |
__ ifloordiv__(self, other) | 定義賦值整數(shù)除法的行為://= |
__ imod__(self, other) | 定義賦值取模算法的行為:%= |
__ ipow__(self, other) | 定義賦值冪運算的行為:**= |
__ ilshift__(self, other) | 定義賦值按位左移位的行為:<<= |
__ irshift__(self, other) | 定義賦值按位右移位的行為:>>= |
__ iand__(self, other) | 定義賦值按位與操作的行為:&= |
__ ixor__(self, other) | 定義賦值按位異或操作的行為:^= |
__ ior__(self, other) | 定義賦值按位或操作的行為:丨= |
- | - |
四、一元操作符
- 一元操作符的魔法方法
魔法方法 | 定義 |
__ neg__(self) | 定義正號的行為:+x |
__ pos__(self) | 定義負號的行為:-x |
__ abs__(self) | 定義當被abs()調用時的行為 |
__ invert__(self) | 定義按位求反的行為:~x |
– | – |
到此這篇關于總結Python常用的魔法方法的文章就介紹到這了,更多相關Python魔法方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談numpy.where() 的用法和np.argsort()的用法說明
這篇文章主要介紹了淺談numpy.where() 的用法和np.argsort()的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05Python?pandas數(shù)據(jù)合并merge函數(shù)用法詳解
這篇文章主要給大家介紹了關于Python?pandas數(shù)據(jù)合并merge函數(shù)用法的相關資料,數(shù)據(jù)分析中經常會遇到數(shù)據(jù)合并的基本問題,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07