運(yùn)算符 | 對(duì)應(yīng)的魔法方法 | 中文注釋 |
+ | __ 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ù)運(yùn)算結(jié)果結(jié)合,divmod(a,b)返回值是一個(gè)元組(a//b, a%b) |
** | __ pow__(self, other[,modulo]) | self的other次方再對(duì)modulo取余 |
__ lshift__(self, other) | 按位左移 | |
>> | __ rshift__(self, other) | 按位右移 |
__ and__(self, other) | 按位與操作 | |
^ | __ xor__(self, other) | 按位異或操作(同為0,異為1) |
丨 | __ or__(self, other) | 按位或操作(有1則1) |
– | – | – |
>>> type(len) class 'builtin_function_or_method'> #普通的BIF >>> type(int) class 'type'> #工廠函數(shù)(類對(duì)象),當(dāng)調(diào)用它們的時(shí)候,其實(shí)就是創(chuàng)建了一個(gè)相應(yīng)的實(shí)例對(duì)象 >>> type(dir) class 'builtin_function_or_method'> >>> type(list) class 'type'> >>> a = int('123') #創(chuàng)建一個(gè)相應(yīng)的實(shí)例對(duì)象a >>> b = int('345') >>> a + b #python在兩個(gè)對(duì)象進(jìn)行相加操作 468
繼承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 #兩個(gè)對(duì)象相加,觸發(fā) __add__(self,other)方法 -2 >>> a - b 8 >>> 實(shí)例2:錯(cuò)誤寫法,會(huì)造成無限遞歸 >>> 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強(qiáng)制轉(zhuǎn)換為整型,所以不會(huì)出現(xiàn)兩個(gè)對(duì)象相加觸發(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) | 定義加法的行為:+(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rsub__(self, other) | 定義減法的行為:-(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rmul__(self, other) | 定義乘法的行為:*(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rtruediv__(self, other) | 定義真除法的行為:/(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rfloordiv__(self, other) | 定義整數(shù)除法的行為://(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rmod__(self, other) | 定義取模算法的行為:%(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rdivmod__(self, other) | 定義當(dāng)被divmod()調(diào)用時(shí)的行為(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rpow__(self, other) | 定義當(dāng)被power()調(diào)用或**運(yùn)算時(shí)的行為(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rlshift__(self, other) | 定義按位左移位的行為:(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rrshift__(self, other) | 定義按位右移位的行為:>>(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rand__(self, other) | 定義按位與操作的行為:(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ rxor__(self, other) | 定義按位異或操作的行為:^(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
__ ror__(self, other) | 定義按位或操作的行為:丨(當(dāng)左操作數(shù)不支持相應(yīng)的操作時(shí)被調(diào)用) |
– | – |
>>> class int(int): def __add__(self,other): return int.__sub__(self,other) >>> a = int(3) >>> b = int(2) >>> a + b 1 反運(yùn)算與算術(shù)運(yùn)算符的不同之處是,反運(yùn)算多了一個(gè)'r',例如 __add__()的反運(yùn)算對(duì)應(yīng)為 __radd__() >>> a + b 這里a是加數(shù),b是被加數(shù),如果a對(duì)象的__add__()方法沒有實(shí)現(xiàn)或者不支持相應(yīng)的操作,那么python就會(huì)自動(dòng)調(diào)用b的__radd__()方法 實(shí)例: >>> class Nint(int): def __radd__(self,other): return int.__sub__(self,other) >>> a = Nint(5) >>> b = Nint(3) >>> a + b #由于a對(duì)象默認(rèn)有__add__()方法,所以b的__radd__()沒有執(zhí)行 8 實(shí)例2: >>> class Nint(int): def __radd__(self,other): return int.__sub__(self,other) >>> b = Nint(5) >>> 3 + b #由于3無__add__()方法,所以執(zhí)行b的反運(yùn)算__radd__(self,other)方法,其中self是b對(duì)象 2
eg:注:在重寫反運(yùn)算魔法方法時(shí),一定要注意順序問題。得到的應(yīng)該是個(gè)負(fù)數(shù),所以順序改變下。
增量賦值運(yùn)算的魔法方法
魔法方法 | 定義 |
__ iadd__(self, other) | 定義賦值加法的行為:+= |
__ isub__(self, other) | 定義賦值減法的行為:-= |
__ imul__(self, other) | 定義賦值乘法的行為:*= |
__ itruediv__(self, other) | 定義賦值真除法的行為:/= |
__ ifloordiv__(self, other) | 定義賦值整數(shù)除法的行為://= |
__ imod__(self, other) | 定義賦值取模算法的行為:%= |
__ ipow__(self, other) | 定義賦值冪運(yùn)算的行為:**= |
__ ilshift__(self, other) | 定義賦值按位左移位的行為:= |
__ irshift__(self, other) | 定義賦值按位右移位的行為:>>= |
__ iand__(self, other) | 定義賦值按位與操作的行為:= |
__ ixor__(self, other) | 定義賦值按位異或操作的行為:^= |
__ ior__(self, other) | 定義賦值按位或操作的行為:丨= |
- | - |
魔法方法 | 定義 |
__ neg__(self) | 定義正號(hào)的行為:+x |
__ pos__(self) | 定義負(fù)號(hào)的行為:-x |
__ abs__(self) | 定義當(dāng)被abs()調(diào)用時(shí)的行為 |
__ invert__(self) | 定義按位求反的行為:~x |
– | – |
到此這篇關(guān)于總結(jié)Python常用的魔法方法的文章就介紹到這了,更多相關(guān)Python魔法方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:常州 駐馬店 宿遷 蘭州 江蘇 山東 成都 六盤水
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《總結(jié)Python常用的魔法方法》,本文關(guān)鍵詞 總結(jié),Python,常用的,常,用的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。