我們在調(diào)用python類中的某個方法時,通常會看到某些特殊的方法,它們總被雙下劃線所包圍,像這種格式:"__方法名__",這些方法很強大,充滿魔力,可以讓你實現(xiàn)很多功能。,如果你的對象實現(xiàn)(重載)了這些方法中的某一個,那么這個方法就會在特殊的情況下被 Python 所調(diào)用,你可以定義自己想要的行為,而這一切都是自動發(fā)生的。因此了解這類方法的作用及用戶很有必要,以下對基本魔法方法做出總結(jié),請看表格:
魔法方法 | 含義 | |
---|---|---|
? | 基本的魔法方法 | |
new(cls[, ...]) | 1. new 是在一個對象實例化的時候所調(diào)用的第一個方法 2. 它的第一個參數(shù)是這個類,其他的參數(shù)是用來直接傳遞給 init 方法 3. new 決定是否要使用該 init 方法,因為 new 可以調(diào)用其他類的構(gòu)造方法或者直接返回別的實例對象來作為本類的實例,如果 new 沒有返回實例對象,則 init 不會被調(diào)用 4. new 主要是用于繼承一個不可變的類型比如一個 tuple 或者 string |
|
init(self[, ...]) | 構(gòu)造器,當一個實例被創(chuàng)建的時候調(diào)用的初始化方法 | |
del(self) | 析構(gòu)器,當一個實例被銷毀的時候調(diào)用的方法 | |
call(self[, args...]) | 允許一個類的實例像函數(shù)一樣被調(diào)用:x(a, b) 調(diào)用 x.call(a, b) | |
len(self) | 定義當被 len() 調(diào)用時的行為 | |
repr(self) | 定義當被 repr() 調(diào)用時的行為 | |
str(self) | 定義當被 str() 調(diào)用時的行為 | |
bytes(self) | 定義當被 bytes() 調(diào)用時的行為 | |
hash(self) | 定義當被 hash() 調(diào)用時的行為 | |
bool(self) | 定義當被 bool() 調(diào)用時的行為,應(yīng)該返回 True 或 False | |
format(self, format_spec) | 定義當被 format() 調(diào)用時的行為 | |
? | 有關(guān)屬性 | |
getattr(self, name) | 定義當用戶試圖獲取一個不存在的屬性時的行為 | |
getattribute(self, name) | 定義當該類的屬性被訪問時的行為 | |
setattr(self, name, value) | 定義當一個屬性被設(shè)置時的行為 | |
delattr(self, name) | 定義當一個屬性被刪除時的行為 | |
dir(self) | 定義當 dir() 被調(diào)用時的行為 | |
get(self, instance, owner) | 定義當描述符的值被取得時的行為 | |
set(self, instance, value) | 定義當描述符的值被改變時的行為 | |
delete(self, instance) | 定義當描述符的值被刪除時的行為 | |
? | 比較操作符 | |
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) | |
? | 定義運算符 | |
add(self, other) | 定義加法的行為:+ | |
sub(self, other) | 定義減法的行為:- | |
mul(self, other) | 定義乘法的行為:* | |
truediv(self, other) | 定義真除法的行為:/ | |
floordiv(self, other) | 定義整數(shù)除法的行為:// | |
mod(self, other) | 定義取模算法的行為:% | |
divmod(self, other) | 定義當被 divmod() 調(diào)用時的行為 | |
pow(self, other[, modulo]) | 定義當被 power() 調(diào)用或 ** 運算時的行為 | |
lshift(self, other) | 定義按位左移位的行為:<< | |
rshift(self, other) | 定義按位右移位的行為:>> | |
and(self, other) | 定義按位與操作的行為:& | |
xor(self, other) | 定義按位異或操作的行為:^ | |
or(self, other) | 定義按位或操作的行為:| | |
? | 反運算 | |
radd(self, other) rsub(self, other) rmul(self, other) rtruediv(self, other) rfloordiv(self, other) rmod(self, other) rdivmod(self, other) rpow(self, other) rlshift(self, other) rrshift(self, other) rxor(self, other) ror(self, other) |
(與上方相同,當左操作數(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[, modulo]) | 定義賦值冪運算的行為:**= | |
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 | |
? | 類型轉(zhuǎn)換 | |
complex(self) | 定義當被 complex() 調(diào)用時的行為(需要返回恰當?shù)闹担?/td> | |
int(self) | 定義當被 int() 調(diào)用時的行為(需要返回恰當?shù)闹担?/td> | |
float(self) | 定義當被 float() 調(diào)用時的行為(需要返回恰當?shù)闹担?/td> | |
round(self[, n]) | 定義當被 round() 調(diào)用時的行為(需要返回恰當?shù)闹担?/td> | |
index(self) | 1. 當對象是被應(yīng)用在切片表達式中時,實現(xiàn)整形強制轉(zhuǎn)換 2. 如果你定義了一個可能在切片時用到的定制的數(shù)值型,你應(yīng)該定義 index 3. 如果 index 被定義,則 int 也需要被定義,且返回相同的值 |
|
? | 上下文管理 | |
enter(self) | 1. 定義當使用 with 語句時的初始化行為 2. enter 的返回值被 with 語句的目標或者 as 后的名字綁定 |
|
exit(self, exc_type, exc_value, traceback) | 1. 定義當一個代碼塊被執(zhí)行或者終止后上下文管理器應(yīng)該做什么 2. 一般被用來處理異常,清除工作或者做一些代碼塊執(zhí)行完畢之后的日常工作 |
|
? | 容器類型 | |
len(self) | 定義當被 len() 調(diào)用時的行為(返回容器中元素的個數(shù)) | |
getitem(self, key) | 定義獲取容器中指定元素的行為,相當于 self[key] | |
setitem(self, key, value) | 定義設(shè)置容器中指定元素的行為,相當于 self[key] = value | |
delitem(self, key) | 定義刪除容器中指定元素的行為,相當于 del self[key] | |
iter(self) | 定義當?shù)萜髦械脑氐男袨?/td> | |
reversed(self) | 定義當被 reversed() 調(diào)用時的行為 | |
contains(self, item) | 定義當使用成員測試運算符(in 或 not in)時的行為 |
以上內(nèi)容非原創(chuàng),摘自博客園:https://www.cnblogs.com/seablog/p/7173107.html