Python中類方法過載---大部分

weixin_30922589發表於2017-12-09

過載方法格式:
def __xxx__(self,other):
...
注:過載方法格式
-----------------------------------------------------------------運算子
運算子過載:
作用:
讓自定義的類建立的物件像內建物件一樣進項運算子操作
算數運算子:
__add__ 加法 +
__sub__ 減法 -
__mul__ 乘法 *
__truedif__ 除法 /
__floordiv__ 地板除 //
__mod__ 取模(求餘) %
__pow__ 冪 **

反向算數運算子過載:
__radd__(self, lhs) # 加法 lhs + self
__rsub__(self, lhs) # 減法 lhs + self
__rmul__(self, lhs) # 乘法 lhs * self
__rtruediv__(self, lhs) # 除法 lhs / self
__rfloordiv__(self, lhs) # 地板除 lhs // self
__rmod__(self, lhs) # 取模 lhs % self
__rpow__(self, lhs) # 冪運算 lhs ** self
注:lhs(left hand side) 左手邊

複合賦值算數運算子的過載:
__iadd__(self, other) # 加法 self += other
__isub__(self, other) # 減法 self -= other
__imul__(self, other) # 乘法 self *= other
__itruediv__(self, other) # 除法 self /= other
__ifloordiv__(self, other) # 地板除 self //= other
__imod__(self, other) # 取模 self %= other
__ipow__(self, other) # 冪運算 self **= other
注:當過載後優先使用過載的方法,否則使用__add__等方法代替
-----------------------------------------------------------------比較運算子
比較運算子過載:
__lt__ 小於 <
__le__ 大於等於 <=
__gt__ 大於 >
__ge__ 大於等於 >=
__eq__ 等於 ==
__ne__ 不等於 !=
-----------------------------------------------------------------位操作運算子
位操作運算子過載:
__and__ 位與 &
__or__ 位或 |
__xor__ 位異或 ^
__lshift__ 左移 <<
__rshift__ 右移 >>

反向位操作運算子:
__rand__ 位與 &
__ror__ 位或 |
__rxor__ 位異或 ^
__rlshift__ 左移 <<
__rrshift__ 右移 >>

複合賦值位運算子過載:
__iand__ 位與 &
__ior__ 位或 |
__ixor__ 位異或 ^
__ilshift__ 左移 <<
__irshift__ 右移 >>
-----------------------------------------------------------------一元運算子
一元運算子的過載:
__neg__ 符號 -
__pos__ 正號 +
__invert__ 取反 ~
過載格式:
def __xxx__(self):
pass
-----------------------------------------------------------------內建函式
內建函式過載:
def __abs__(self) abs(obj) 函式呼叫
def __len__(self) len(obj) 函式呼叫
def __reversed__(self) reversed(obj) 函式呼叫
def __round__(self) round(obj) 函式呼叫
-----------------------------------------------------------------數值轉換函式
數值轉換函式過載:
__int__ int(obj)
__float__ float(obj)
__complex__ complex(obj)
__bool__ bool(obj)
-----------------------------------------------------------------布林測試運算子
布林測試運算子過載:
格式:
def __bool__(self):
....
作用:
1) 用於bool(obj) 函式取值
2) 用於if語句真值表示式中
3) 用於while語句真值表示式中
過載說明:
當沒有 __bool__(self) 方法時,真值測試將取
__len__(self) 方法的返回值來測試布林值
-----------------------------------------------------------------in / not in
in / not in 運算子過載:
格式:
def __contains__(self, e):
...
作用:
成員資格測試(通常)
-----------------------------------------------------------------索引和切片
索引和切片運算子的過載:
過載方法:
__getitem__(self, i) 方法
__sefitem__(self, i, v) 方法
__delitem__(self, i) 方法
作用:
讓自定義型別的物件能進行索引和切片操作
切片(slice)過載:
切片過載同性索引過載公用的方法
__getitem__(self, i) 切片取值
__sefitem__(self, i, v) 切片賦值
__delitem__(self, i) del切片刪除
-----------------------------------------------------------------迭代器過載
迭代器:
__next__(self):
可迭代物件:
__iter__(self):
-----------------------------------------------------------------with環境管理器類內過載
類內有__enter__ 和 __exit__ 方法的類被稱為環境管理器
能夠用with進行管理的物件必須是環境管理器
__enter__ 方法將在進入 with 語句時被呼叫返回由 as 變數管理的物件
__exit__ 方法將在離開with語句時被呼叫,且可以用引數來判斷離開with語句時是否有異常發生並作出相應的處理

轉載於:https://www.cnblogs.com/laolibk/p/8011472.html

相關文章