Try   HackMD

Python Magic Method List | Python 魔術方法列表


Building Class | 構造類別

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

Attribute | 屬性

Method Name Symbol Description
__getattr__(self, key) self.key 調用屬性
__setattr__(self, key, value) self.key = value 設定屬性
__delattr__(self, key) del self.key 刪除屬性

Boolean Operation | 布林值

Method Name Symbol Description
__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 Name Symbol Description
__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 Name Symbol Description
__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 Name Symbol Description
__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 Name Symbol Description
__pos__(self) +self
__neg__(self) -self
__abs__(self) abs(self) 絕對值
__invert__(self) ~self

Type Conversion | 類型轉換

Method Name Symbol Description
__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 Manage | 上下文管理器

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

Container | 容器

Method Name Symbol Description
__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 在/不在容器裡

Dark Theme License

The MIT License (MIT)

Copyright © 2022-2024 Lumynous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.