校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 總結(jié)Python常用的魔法方法

總結(jié)Python常用的魔法方法

熱門標簽:河北防封卡電銷卡 400電話辦理哪種 手機網(wǎng)頁嵌入地圖標注位置 應(yīng)電話機器人打電話違法嗎 開封語音外呼系統(tǒng)代理商 開封自動外呼系統(tǒng)怎么收費 電銷機器人的風險 天津電話機器人公司 地圖標注線上如何操作

一、算數(shù)運算符的魔法方法

  • python2.2以后,對類和類型進行了統(tǒng)一,做法就是講int()、float()、str()、list()、tuple()這些BIF轉(zhuǎn)換為工廠函數(shù)(類對象)
  • 給出以下算數(shù)運算符對應(yīng)的魔法方法,前面和后面都被雙下劃線包尾說明是魔法方法
運算符 對應(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ù)運算結(jié)果結(jié)合,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ù)(類對象),當調(diào)用它們的時候,其實就是創(chuàng)建了一個相應(yīng)的實例對象
>>> type(dir)
class 'builtin_function_or_method'>
>>> type(list)
class 'type'>

>>> a = int('123')        #創(chuàng)建一個相應(yī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強制轉(zhuǎn)換為整型,所以不會出現(xiàn)兩個對象相加觸發(fā)__add__()方法
    def __sub__(self,other):
        return (int(self) - int(other))

    
>>> a = New_int(3)
>>> b = New_int(5)
>>> a + b
8

二、反運算相關(guān)的魔法方法

  • 反運算相關(guān)的魔法方法
魔法方法 定義
__ radd__(self, other) 定義加法的行為:+(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rsub__(self, other) 定義減法的行為:-(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rmul__(self, other) 定義乘法的行為:*(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rtruediv__(self, other) 定義真除法的行為:/(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rfloordiv__(self, other) 定義整數(shù)除法的行為://(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rmod__(self, other) 定義取模算法的行為:%(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rdivmod__(self, other) 定義當被divmod()調(diào)用時的行為(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rpow__(self, other) 定義當被power()調(diào)用或**運算時的行為(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rlshift__(self, other) 定義按位左移位的行為:(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rrshift__(self, other) 定義按位右移位的行為:>>(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rand__(self, other) 定義按位與操作的行為:(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ rxor__(self, other) 定義按位異或操作的行為:^(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ ror__(self, other) 定義按位或操作的行為:丨(當左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
>>> class int(int):
    def __add__(self,other):
        return int.__sub__(self,other)

    
>>> a = int(3)
>>> b = int(2)
>>> a + b
1

反運算與算術(shù)運算符的不同之處是,反運算多了一個'r',例如 __add__()的反運算對應(yīng)為 __radd__()

>>> a + b

這里a是加數(shù),b是被加數(shù),如果a對象的__add__()方法沒有實現(xiàn)或者不支持相應(yīng)的操作,那么python就會自動調(diào)用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:注:在重寫反運算魔法方法時,一定要注意順序問題。得到的應(yīng)該是個負數(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()調(diào)用時的行為
__ invert__(self) 定義按位求反的行為:~x

到此這篇關(guān)于總結(jié)Python常用的魔法方法的文章就介紹到這了,更多相關(guān)Python魔法方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解Python魔法方法之描述符類
  • Python面向?qū)ο竽Хǚ椒ê蛦卫K代碼實例
  • Python魔法方法 容器部方法詳解
  • Python類中的魔法方法之 __slots__原理解析
  • Python魔法方法功能與用法簡介
  • Python魔法方法詳解
  • python魔法方法-屬性訪問控制詳解
  • python魔法方法-屬性轉(zhuǎn)換和類的表示詳解
  • python魔法方法-自定義序列詳解
  • Python中的魔法方法深入理解
  • 詳解Python常用的魔法方法

標簽:常州 駐馬店 宿遷 蘭州 江蘇 山東 成都 六盤水

巨人網(wǎng)絡(luò)通訊聲明:本文標題《總結(jié)Python常用的魔法方法》,本文關(guān)鍵詞  總結(jié),Python,常用的,常,用的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《總結(jié)Python常用的魔法方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于總結(jié)Python常用的魔法方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 北京市| 中卫市| 孟州市| 镇安县| 施秉县| 新余市| 江孜县| 页游| 兴安盟| 东乡| 鱼台县| 平昌县| 清丰县| 洪泽县| 邛崃市| 洞头县| 开平市| 大足县| 微博| 铜山县| 壶关县| 富裕县| 浙江省| 闻喜县| 建德市| 长寿区| 子洲县| 桂平市| 广河县| 阜平县| 浦江县| 涞源县| 噶尔县| 曲水县| 泰兴市| 商都县| 喜德县| 襄城县| 鹿邑县| 长春市| 措美县|