Python 学习笔记 -- 类的魔法方法

常用魔法方法 含义
 __new__(cls[,...])

 1.__new__在对象被实例化时调用

 2.第一个参数是类本身,其他参数传入__init__中

 3.__new__如果没有返回值,则不会调用__init__

 4.主要用于继承不可变类型时重写

__init__(self[,..])   1.构造器 
__del__(self)   1.析构器 
__call__(self[,args...])   1.允许一个类的实例想函数一样被调用:a(x,y) == a.__call__(x,y) 
__len__(self)   1.当被len()时调用
__repr__(self)  1.当被repr()时调用
__str__(self)   1.当被str()时调用 
 __bytes__(self)  1.当被bytes()时调用 
__hash__(self)   1.当被hash()时调用 
__bool__(self)   1.当被bool()时调用 
 __format__(self,format__spec)  1.当被format()时调用 
 有关属性  
__getattr__(self,name)   1.当用户试图获取不存在的属性时调用
__getattrbute__(self,name)   1.当类的属性被访问时的行为 
__setattr__(self,name)   1.当一个属性被设置时的行为 
__delattr__(self,name)   1.当一个属性被删除是的行为 
__dir__(self)   1.当dir()被调用时的行为 
__get__(self,instance,owner)   1.当描述符的值被取得时的行为
__set__(self,insetance,value)   1.当描述符的值被设置时的行为 
__delete__(self,instance)   1.当描述符的值被删除时的行为
比较操作符   
 __lt__(self,other)  1.x < y 等同 x.__lt__(y)
__le__(self,other)   1.x <= y 等同 x.__le__(y) 
__eq__(self,other)   1.x == y 等同 x.__eq__(y) 
__ne__(self,other)   1.x != y 等同 x.__ne__(y) 
__gt__(self,other)   1. x > y 等同 x.__gt__(y) 
__ge__(self,other)   1.x >= y 等同 x.__ge__(y) 
算数运算符   这里的都是从左向右的
__add__(self,other)   1.+ 从左向右
__sub__(self,other)   1.-  从左向右
__mul__(self,other)   1.* 从左向右
__truediv__(self,other)   1./  从左向右
__floordiv__(self,other)  1.// 从左向右
__mod__(self,other)   1.% 从左向右
__divmod__(self,other)   1.当divmod()时调用 
__pow__(self,other[,modulo])   1.** 或者 power时调用 
__lshift__(self,other)   1.<< 
__rshift__(self,other)   1.>> 
__and__(self,other)   1.& 
__xor__(self,other)   1.^ 
__or__(self,other)   1. |
 反运算  这里的都是从右向左
__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)   
__rand__(self,other)  
__rxor__(self,other)   
__ror__(self,other)  
增量运算  
__iadd__(self,other)  1.+=
__isub__(self,other)  1.-=
__imul__(self,other)   1.*+
__itruediv__(self,other)   1./= 
__ifloordiv__(self,other)   1.//= 
__imod__(self,other)   1.%= 
__ipow__(self,other)   1.**= 
__ilshift__(self,other)  1.<<= 
__irshift__(self,other)  1.>>= 
__iand__(self,other)   1.&=
__ixor__(self,other)   1.^=
__ior__(self,other)   1.|= 
 一元操作符  
__pos__(self)  1.+x 
__neg__(self)   1.-x 
__abs__(self)   1.取绝对值abs() 
__invert__(self)  1.取反~
 类型转换  
__complex__(self)   1.complex()时调用(需要返回值)
__int__(self)   1.int()时调用(需要返回值)
__float__(self)   1.float()时调用 (需要返回值)
__round__(self[,n])   1.round()时调用 (需要返回值)
__index__(self)

 1.当对象是被应用在切片表达式中调用

 2.如果__index__被定义,则__int__也需要被定义,且返回相同的值

上下文管理with语句  
__enter__(self) 

 1.定义当使用with语句是的初始化行为

 2.__enter__的返回值将赋值给as后面的名字

__exit__(self,exc_type,exc_value,traceback) 

 1.当代码块结束时的行为

 2.一般用来处理异常,或者清除工作,比如关闭文件之类的 

容器类型  
__len__(self)  1.len()时的行为
__getitem__(self,key)   1.获取容器中指定元素时的行为 
__setitem__(self,key,value)   1.设置容器中指定元素时的行为
__delitem__(self,key)   1.删除容器中指定元素时的行为 
__iter__(self)   1.当迭代时的行为 
__reversed__(self)  1.reversed()时的行为 
__contains__(self,item)   1.in 或 not in 时的行为
   
   
   
   
   
   
   
   
   
原文地址:https://www.cnblogs.com/jiangchenxi/p/8073482.html