溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中的魔法函數(shù)總結(jié)整理

發(fā)布時間:2020-05-23 11:20:42 來源:網(wǎng)絡(luò) 閱讀:3001 作者:BlueMiaomiao 欄目:編程語言
基本魔法方法 功能
__new__(cls[, ...]) 1. new 是在一個對象實例化的時候所調(diào)用的第一個方法 2. 它的第一個參數(shù)是這個類,其他的參數(shù)是用來直接傳遞給 init 方法 3. new 決定是否要使用該 init 方法,因為 new 可以調(diào)用其他類的構(gòu)造方法或者直接返回別的實例對象來作為本類的實例,如果 new 沒有返回實例對象,則 init 不會被調(diào)用 new 主要是用于繼承一個不可變的類型比如一個 tuple 或者 string
__init__(self[, ...]) 構(gòu)造器,當(dāng)一個實例被創(chuàng)建的時候調(diào)用的初始化方法
__del__(self) 析構(gòu)器,當(dāng)一個實例被銷毀的時候調(diào)用的方法
__call__(self[, args...]) 允許一個類的實例像函數(shù)一樣被調(diào)用:x(a, b) 調(diào)用 x.call(a, b)
__len__(self) 定義當(dāng)被 len() 調(diào)用時的行為
__repr__(self) 定義當(dāng)被 repr() 調(diào)用時的行為
__bytes__(self) 定義當(dāng)被 bytes() 調(diào)用時的行為
__str__(self) 定義當(dāng)被 str() 調(diào)用時的行為
__hash__(self) 定義當(dāng)被 hash() 調(diào)用時的行為
__bool__(self) 定義當(dāng)被 bool() 調(diào)用時的行為,應(yīng)該返回 True 或 False
__format__(self, format_spec) 定義當(dāng)被 format() 調(diào)用時的行為
屬性相關(guān)魔法方法 功能
__getattr__(self, name) 定義當(dāng)用戶試圖獲取一個不存在的屬性時的行為
__getattribute__(self, name) 定義當(dāng)該類的屬性被訪問時的行為
__setattr__(self, name, value) 定義當(dāng)一個屬性被設(shè)置時的行為
__delattr__(self, name) 定義當(dāng)一個屬性被刪除時的行為
__dir__(self) 定義當(dāng) dir() 被調(diào)用時的行為
__get__(self, instence, owner) 定義當(dāng)描述符的值被取得時的行為
__set__(self, instence, value) 定義當(dāng)描述符的值被改變時的行為
__delete__(self, instence) 定義當(dāng)描述符的值被刪除時的行為
比較操作符魔法方法 功能
__lt__(self, other) 定義小于號的行為:x < y 調(diào)用 x.lt(y)
__le__(self, other) 定義小于等于號的行為:x <= y 調(diào)用 x.le(y)
__eq__(self, other) 定義等于號的行為:x == y 調(diào)用 x.eq(y)
__ne__(self, other) 定義不等號的行為:x != y 調(diào)用 x.ne(y)
__gt__(self, other) 定義大于號的行為:x > y 調(diào)用 x.gt(y)
__ge__(self, other) 定義大于等于號的行為:x >= y 調(diào)用 x.ge(y)
算數(shù)運算魔法方法 功能
__add__(self, other) 定義加法的行為:+
__sub__(self, other) 定義減法的行為:-
__mul__(self, other) 定義乘法的行為:*
__truediv__(self, other) 定義真除法的行為:/
__floordiv__(self, other) 定義整數(shù)除法的行為://
__mod__(self, other) 定義取模算法的行為:%
__divmod__(self, other) 定義當(dāng)被 divmod() 調(diào)用時的行為
__pow__(self, other[, modulo]) 定義當(dāng)被 power() 調(diào)用或 ** 運算時的行為
__lshift__(self, other) 定義按位左移位的行為:<<
__rshift__(self, other) 定義按位右移位的行為:>>
__and__(self, other) 定義按位與操作的行為:&
__xor__(self, other) 定義按位異或操作的行為:^
__or__(self, other) 定義按位或操作的行為:\
反運算魔法方法 功能
__radd__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rsub__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rmul__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rtruediv__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rfloordiv__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rmod__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rdivmod__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rpow__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rlshift__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rrshift__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__rxor__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
__ror__(self, other) (與上方相同,當(dāng)左操作數(shù)不支持相應(yīng)的操作時被調(diào)用)
增量賦值運算魔法方法 功能
__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) 定義負(fù)號的行為:-x
__abs__(self) 定義當(dāng)被 abs() 調(diào)用時的行為
__invert__(self) 定義按位求反的行為:~x
類型轉(zhuǎn)換魔法方法 功能
__complex__(self) 定義當(dāng)被 complex() 調(diào)用時的行為(需要返回恰當(dāng)?shù)闹担?/td>
__int__(self) 定義當(dāng)被 int() 調(diào)用時的行為(需要返回恰當(dāng)?shù)闹担?/td>
__float__(self) 定義當(dāng)被 float() 調(diào)用時的行為(需要返回恰當(dāng)?shù)闹担?/td>
__round__(self[, n]) 定義當(dāng)被 round() 調(diào)用時的行為(需要返回恰當(dāng)?shù)闹担?/td>
__index__(self) 當(dāng)對象是被應(yīng)用在切片表達(dá)式中時,實現(xiàn)×××強制轉(zhuǎn)換 2. 如果你定義了一個可能在切片時用到的定制的數(shù)值型,你應(yīng)該定義 index 3. 如果 index 被定義,則 int 也需要被定義,且返回相同的值
上下文管理(with 語句)相關(guān)魔法方法 功能
__enter__(self) 1.定義當(dāng)使用 with 語句時的初始化行為 2. enter 的返回值被 with 語句的目標(biāo)或者 as 后的名字綁定
__exit__(self, exc_type, exc_value, traceback) 1.定義當(dāng)一個代碼塊被執(zhí)行或者終止后上下文管理器應(yīng)該做什么 2. 一般被用來處理異常,清除工作或者做一些代碼塊執(zhí)行完畢之后的日常工作
容器相關(guān)魔法方法 功能
__len__(self) 定義當(dāng)被 len() 調(diào)用時的行為(返回容器中元素的個數(shù))
__getitiem__(self, key) 定義獲取容器中指定元素的行為,相當(dāng)于 self[key]
__setitem__(self, key, value) 定義設(shè)置容器中指定元素的行為,相當(dāng)于 self[key] = value
__delitem__(self, key) 定義刪除容器中指定元素的行為,相當(dāng)于 del self[key]
__iter__(self) 定義當(dāng)?shù)萜髦械脑氐男袨?/td>
__reversed__(self) 定義當(dāng)被 reversed() 調(diào)用時的行為
__contains__(self, item) 定義當(dāng)使用成員測試運算符(in 或 not in)時的行為
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI