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

本文详细介绍了 Python 中常用的魔法方法,包括对象创建、销毁、调用、属性访问及各种运算符的重载实现。这些方法使得 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值