python的魔术方法大全
<div class="t_fsz"><p> 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”(魔术方法),例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,这边给大家把所有的魔术方法汇总了一下,希望对大家的学习有所帮助。</p>
<p> </p>
<p> </p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td id="postmessage_1932013" class="t_f">
<p style="text-align: center"><span style="font-size: medium"><br></span><span style="color: rgba(0, 128, 0, 1)"><strong><span style="font-size: 18pt">python的魔术方法大全</span></strong></span></p>
<span style="font-size: medium"><br></span>
<table class="t_table" cellspacing="0">
<tbody>
<tr>
<td width="30%">
<div align="center"><strong><span style="font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1); font-size: 18pt">魔法方法</span></strong></div>
</td>
<td>
<div align="center"><strong><span style="font-family: 宋体; font-size: 18pt; color: rgba(0, 128, 0, 1)">含义</span></strong></div>
</td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><strong><span style="color: rgba(0, 128, 0, 1); font-family: "Microsoft YaHei"; font-size: 18px">基本的魔法方法</span></strong></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__new__(cls[, ...])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">1. __new__ 是在一个对象实例化的时候所调用的第一个方法</span><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)"><br></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__init__(self[, ...])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">构造器,当一个实例被创建的时候调用的初始化方法</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__del__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">析构器,当一个实例被销毁的时候调用的方法</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__call__(self[, args...])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">允许一个类的实例像函数一样被调用:x(a, b) 调用 x.__call__(a, b)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__len__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 len() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__repr__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 repr() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__str__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 str() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__bytes__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 bytes() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__hash__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 hash() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__bool__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 bool() 调用时的行为,应该返回 True 或 False</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__format__(self, format_spec)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 format() 调用时的行为</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><span style="font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1); font-size: 18px"> </span><strong><span style="color: rgba(0, 128, 0, 1); font-family: "Microsoft YaHei"; font-size: 18px">有关属性</span></strong></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__getattr__(self, name)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当用户试图获取一个不存在的属性时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__getattribute__(self, name)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当该类的属性被访问时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__setattr__(self, name, value)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当一个属性被设置时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__delattr__(self, name)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当一个属性被删除时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__dir__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当 dir() 被调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__get__(self, instance, owner)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当描述符的值被取得时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__set__(self, instance, value)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当描述符的值被改变时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__delete__(self, instance)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当描述符的值被删除时的行为</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px; color: rgba(0, 128, 0, 1)"> 比较操作符</span></strong></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__lt__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义小于号的行为:x < y 调用 x.__lt__(y)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__le__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义小于等于号的行为:x <= y 调用 x.__le__(y)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__eq__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义等于号的行为:x == y 调用 x.__eq__(y)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ne__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义不等号的行为:x != y 调用 x.__ne__(y)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__gt__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义大于号的行为:x > y 调用 x.__gt__(y)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ge__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义大于等于号的行为:x >= y 调用 x.__ge__(y)</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><strong><span style="font-size: 18px; color: rgba(0, 128, 0, 1)"><span style="font-family: "Microsoft YaHei""> </span><span style="font-family: 宋体">算数运算符</span></span></strong></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__add__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义加法的行为:+</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__sub__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义减法的行为:-</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__mul__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义乘法的行为:*</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__truediv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义真除法的行为:/</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__floordiv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义整数除法的行为://</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__mod__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义取模算法的行为:%</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__divmod__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 divmod() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__pow__(self, other[, modulo])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 power() 调用或 ** 运算时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__lshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位左移位的行为:<<</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位右移位的行为:>></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__and__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位与操作的行为:&</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__xor__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位异或操作的行为:^</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__or__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位或操作的行为:|</span></td>
</tr>
<tr>
<td colspan="2"><span style="font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1); font-size: 18px"> </span><span style="color: rgba(0, 128, 0, 1)"><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">反运算</span></strong></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__radd__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rsub__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rmul__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rtruediv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rfloordiv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rmod__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rdivmod__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rpow__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rlshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rrshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rand__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__rxor__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ror__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">(与上方相同,当左操作数不支持相应的操作时被调用)</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><span style="font-family: "Microsoft YaHei"; font-size: 18px; color: rgba(0, 128, 0, 1)"><strong>增量赋值运算</strong></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__iadd__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值加法的行为:+=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__isub__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值减法的行为:-=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__imul__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值乘法的行为:*=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__itruediv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值真除法的行为:/=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ifloordiv__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值整数除法的行为://=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__imod__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值取模算法的行为:%=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ipow__(self, other[, modulo])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值幂运算的行为:**=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ilshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值按位左移位的行为:<<=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__irshift__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值按位右移位的行为:>>=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__iand__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值按位与操作的行为:&=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ixor__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值按位异或操作的行为:^=</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__ior__(self, other)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义赋值按位或操作的行为:|=</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px"> </span></strong></span><span style="font-size: 18px; font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1)"><strong>一元操作符</strong></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__pos__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义正号的行为:+x</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__neg__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义负号的行为:-x</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__abs__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 abs() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__invert__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义按位求反的行为:~x</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><strong><span style="font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1); font-size: 18px"> </span></strong><strong><span style="color: rgba(0, 128, 0, 1); font-family: "Microsoft YaHei"; font-size: 18px">类型转换</span></strong></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__complex__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 complex() 调用时的行为(需要返回恰当的值)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__int__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 int() 调用时的行为(需要返回恰当的值)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__float__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 float() 调用时的行为(需要返回恰当的值)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__round__(self[, n])</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 round() 调用时的行为(需要返回恰当的值)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__index__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">1. 当对象是被应用在切片表达式中时,实现整形强制转换</span><br><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__</span><br><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">3. 如果 __index__ 被定义,则 __int__ 也需要被定义,且返回相同的值</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px"> </span></strong></span></td>
<td>
<div align="center"><span style="color: rgba(51, 51, 153, 1); font-family: 宋体; font-size: 14px">上下文管理(with 语句)</span></div>
</td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__enter__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">1. 定义当使用 with 语句时的初始化行为</span><br><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">2. __enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__exit__(self, exc_type, exc_value, traceback)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么</span><br><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">2. 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作</span></td>
</tr>
<tr>
<td style="text-align: center" colspan="2"><span style="font-family: "Microsoft YaHei"; color: rgba(0, 128, 0, 1); font-size: 18px"><strong> </strong></span><span style="color: rgba(0, 128, 0, 1)"><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">容器类型</span></strong></span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__len__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 len() 调用时的行为(返回容器中元素的个数)</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__getitem__(self, key)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义获取容器中指定元素的行为,相当于 self</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__setitem__(self, key, value)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义设置容器中指定元素的行为,相当于 self = value</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__delitem__(self, key)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义删除容器中指定元素的行为,相当于 del self</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__iter__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当迭代容器中的元素的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__reversed__(self)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当被 reversed() 调用时的行为</span></td>
</tr>
<tr>
<td><span style="font-family: "Microsoft YaHei"; color: rgba(153, 51, 0, 1)"><strong><span style="font-size: 14px">__contains__(self, item)</span></strong></span></td>
<td><span style="font-family: 宋体; font-size: 14px; color: rgba(51, 51, 153, 1)">定义当使用成员测试运算符(in 或 not in)时的行为</span></td>
</tr>
</tbody>
</table>
<br><br></td>
</tr>
</tbody>
</table>
</div>
<div id="comment_1932013" class="cm"> </div>
<h3 class="psth xs1"><span class="icon_ring vm"> </span></h3>
<dl id="ratelog_1932013" class="rate"><dd>
<div id="post_rate_1932013"> </div>
</dd></dl>
</div>
<div id="MySignature" role="contentinfo">
<font color="red">=====请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/nmb-musen/,谢谢!!===== </font><br>
<li><font color="red">=====有任何疑问,欢迎加微信 MoX66999(请备注:博客园-木森)=====</font></li><br><br><br>
来源:https://www.cnblogs.com/nmb-musen/p/10861536.html
頁:
[1]