跳至主要內容

Python 魔術方法列表

芒果凍布丁原創2025年4月18日大约 2 分鐘程式設計PythonPython魔術方法

Building Class |構造類別

Method NameSymbolDescription
__new__(cls, [...)cls([...)構造
__init__(self, [...)self初始化
__del__(self)del self刪除
__call__(self, [...)self([...)調用自身

Attribute |屬性

Method NameSymbolDescription
__getattr__(self, key)self.key獲取屬性
__setattr__(self, key, value)self.key = value設定屬性
__delattr__(self, key)del self.key刪除屬性

Boolean Operation |布林值

Method NameSymbolDescription
__cmp__(self, other)
__eq__(self, other)self == other等於
__ne__(self, other)self != other不等於
__lt__(self, other)self < other小於
__le__(self, other)self <= other小於等於(不大於)
__gt__(self, other)self > other大於
__ge__(self, other)self >= other大於等於(不小於)

Math Operation |數學運算符

Method NameSymbolDescription
__add__(self, other)self + other
__sub__(self, other)self - other
__mul__(self, other)self * other
__truediv__(self, other)self / other
__floordiv__(self, other)self // other整除
__mod__(self, other)self % other求餘
__divmod__(self, other)divmod(self, other)
__pow__(self, other)power(self, other) & self ** other次方
__lshift__(self, other)self << other左移(位元運算)
__rshift__(self, other)self >> other右移(位元運算)
__and__(self, other)self & other與(位元運算)
__or__(self, other)self | other或(位元運算)
__xor__(self, other)self ^ other異或(位元運算)

Right Math Operation |被動數學運算符

Method NameSymbolDescription
__radd__(self, other)other + self被加
__rsub__(self, other)other - self被減
__rmul__(self, other)other * self被乘
__rtruediv__(self, other)other / self被除
__rfloordiv__(self, other)other // self被整除
__rmod__(self, other)other % self被求餘
__rpow__(self, other)other ** self被次方
__rlshift__(self, other)other << self被左移
__rrshift__(self, other)other >> self被右移
__rand__(self, other)other & self被與
__ror__(self, other)other | self被或
__rxor__(self, other)other ^ self被異或

Assign Value Operation |賦值數學運算符

Method NameSymbolDescription
__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自次方
__ilshift__(self, other)self <<= other自左移(位元運算)
__irshift__(self, other)self >>= other自右移(位元運算)
__iand__(self, other)self &= other自與(位元運算)
__ior__(self, other)self |= other自或(位元運算)
__ixor__(self, other)self ^= other自異或(位元運算)

Unary Operation |一元運算符

Method NameSymbolDescription
__pos__(self)+self
__neg__(self)-self
__abs__(self)abs(self)絕對值
__invert__(self)~self逆(位元運算)

Type Conversion |類型轉換

Method NameSymbolDescription
__complex__(self)complex(self)複數
__int__(self)int(self)整數
__float__(self)float(self)浮點數
__round__(self, n])round(self, n])四捨五入
__index__(self)1.
2.
3.
索引
__repr__(self)repr(self)
__bool__(self)bool(self)布林值
__str__(self)str(self)字串
__format__(self)f"{}"

Context Manager |上下文管理器

Method NameSymbolDescription
__enter__(self)1.
2.
進入上下文管理器
__exit__(self, exctype, excvalue, trackback)1.
2.
退出上下文管理器

Container |容器

Method NameSymbolDescription
__len__(self)len(self)容器長度
__getitem__(self, key)self[key]調用容器值
__setitem__(self, key, value)self[key] = value設定容器值
__delitem__(self, key)del self[key]刪除容器值
__iter__(self)iter(self)迭代容器
__reversed__(self)reversed(self)反轉容器
__contains__(self, item)item in self & item not in self在 / 不在容器裡

貢獻者