在Python中以两个下划线开头和结尾的方法,好比:__init__、__str__、__doc__、__new__等,被称为"魔术方法"(Magic methods)。魔术方法在类或对象的某些事件出发后会自动执行,若是但愿根据本身的程序定制本身特殊功能的类,那么就须要对这些方法进行重写。算法
Python 将全部以 __(两个下划线)开头和结尾的类方法保留为魔术方法。因此在定义类方法时,除了上述魔术方法,建议不要以 __ 为前缀。编程
咱们将不一样类型的魔术方法进行归类,那么会分为如下几类。app
魔术方法 | 含义 |
---|---|
__new__(cls[, ...]) | 1. __new__ 是在一个对象实例化的时候所调用的第一个方法 2. 它的第一个参数是这个类,其余的参数是用来直接传递给 __init__ 方法 3. __new__ 决定是否要使用该 __init__ 方法,由于 __new__ 能够调用其余类的构造方法或者直接返回别的实例对象来做为本类的实例,若是 __new__ 没有返回实例对象,则 __init__ 不会被调用 4. __new__ 主要是用于继承一个不可变的类型好比一个 tuple 或者 string |
__init__(self[, ...]) | 构造器,当一个实例被建立的时候调用的初始化方法 |
__del__(self) | 析构器,当一个实例被销毁的时候调用的方法 |
__call__(self[, args...]) | 容许一个类的实例像函数同样被调用:x(a, b) 调用 x.__call__(a, b) |
__len__(self) | 定义当被 len() 调用时的行为 |
__repr__(self) | 定义当被 repr() 调用或者直接执行对象时的行为 |
__str__(self) | 定义当被 str() 调用或者打印对象时的行为 |
__bytes__(self) | 定义当被 bytes() 调用时的行为 |
__hash__(self) | 定义当被 hash() 调用时的行为 |
__bool__(self) | 定义当被 bool() 调用时的行为,应该返回 True 或 False |
__format__(self, format_spec) | 定义当被 format() 调用时的行为 |
__name__ | 类、函数、方法等的名字 |
__module__ | 类定义所在的模块名 |
__class__ | 对象或类所属的类 |
__bases__ | 类的基类元组,顺序为它们在基类列表中出现的顺序 |
__doc__ | 类、函数的文档字符串,若是没有定义则为None |
__mro__ | 类的mro,class.mro()返回的结果保存在__mro__中 |
__dict__ | 类或实例的属性,可写的字典 |
魔术方法 | 含义 |
---|---|
__getattr__(self, name) | 定义当用户试图获取一个不存在的属性时的行为 |
__getattribute__(self, name) | 定义当该类的属性被访问时的行为 |
__setattr__(self, name, value) | 定义当一个属性被设置时的行为 |
__delattr__(self, name) | 定义当一个属性被删除时的行为 |
__dir__(self) | 定义当 dir() 被调用时的行为 |
__get__(self, instance, owner) | 定义当描述符的值被取得时的行为 |
__set__(self, instance, value) | 定义当描述符的值被改变时的行为 |
__delete__(self, instance) | 定义当描述符的值被删除时的行为 |
魔术方法 | 含义 |
---|---|
__lt__(self, other) | 定义小于号的行为:x < y 调用 x.__lt__(y) |
__le__(self, other) | 定义小于等于号的行为:x <= y 调用 x.__le__(y) |
__eq__(self, other) | 定义等于号的行为:x == y 调用 x.__eq__(y) |
__ne__(self, other) | 定义不等号的行为:x != y 调用 x.__ne__(y) |
__gt__(self, other) | 定义大于号的行为:x > y 调用 x.__gt__(y) |
__ge__(self, other) | 定义大于等于号的行为:x >= y 调用 x.__ge__(y) |
魔术方法 | 含义 |
---|---|
__add__(self, other) | 定义加法的行为:+ |
__sub__(self, other) | 定义减法的行为:- |
__mul__(self, other) | 定义乘法的行为:* |
__truediv__(self, other) | 定义真除法的行为:/ |
__floordiv__(self, other) | 定义整数除法的行为:// |
__mod__(self, other) | 定义取模算法的行为:% |
__divmod__(self, other) | 定义当被 divmod() 调用时的行为 |
__pow__(self, other[, modulo]) | 定义当被 power() 调用或 ** 运算时的行为 |
__lshift__(self, other) | 定义按位左移位的行为:<< |
__rshift__(self, other) | 定义按位右移位的行为:>> |
__and__(self, other) | 定义按位与操做的行为:& |
__xor__(self, other) | 定义按位异或操做的行为:^ |
__or__(self, other) | 定义按位或操做的行为: |
魔术方法 | 含义 |
---|---|
__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) | 定义赋值加法的行为:+= |
__isub__(self, other) | 定义赋值减法的行为:-= |
__imul__(self, other) | 定义赋值乘法的行为:*= |
__itruediv__(self, other) | 定义赋值真除法的行为:/= |
__ifloordiv__(self, other) | 定义赋值整数除法的行为://= |
__imod__(self, other) | 定义赋值取模算法的行为:%= |
__ipow__(self, other[, modulo]) | 定义赋值幂运算的行为:**= |
__ilshift__(self, other) | 定义赋值按位左移位的行为:<<= |
__irshift__(self, other) | 定义赋值按位右移位的行为:>>= |
__iand__(self, other) | 定义赋值按位与操做的行为:&= |
__ixor__(self, other) | 定义赋值按位异或操做的行为:^= |
__ior__(self, other) | 定义赋值按位或操做的行为: |
魔术方法 | 含义 |
---|---|
__pos__(self) | 定义正号的行为:+x |
__neg__(self) | 定义负号的行为:-x |
__abs__(self) | 定义当被 abs() 调用时的行为 |
__invert__(self) | 定义按位求反的行为:~x |
魔术方法 | 含义 |
---|---|
__complex__(self) | 定义当被 complex() 调用时的行为(须要返回恰当的值) |
__int__(self) | 定义当被 int() 调用时的行为(须要返回恰当的值) |
__float__(self) | 定义当被 float() 调用时的行为(须要返回恰当的值) |
__round__(self[, n]) | 定义当被 round() 调用时的行为(须要返回恰当的值) |
__index__(self) | 1. 当对象是被应用在切片表达式中时,实现整形强制转换 2. 若是你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__ 3. 若是 __index__ 被定义,则 __int__ 也须要被定义,且返回相同的值 |
魔术方法 | 含义 |
---|---|
__enter__(self) | 1. 定义当使用 with 语句时的初始化行为 2. __enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定 |
__exit__(self, exc_type, exc_value, traceback) | 1. 定义当一个代码块被执行或者终止后上下文管理器应该作什么 2. 通常被用来处理异常,清除工做或者作一些代码块执行完毕以后的平常工做 |
魔术方法 | 含义 |
---|---|
__len__(self) | 定义当被 len() 调用时的行为(返回容器中元素的个数) |
__getitem__(self, key) | 定义获取容器中指定元素的行为,至关于 self[key] |
__setitem__(self, key, value) | 定义设置容器中指定元素的行为,至关于 self[key] = value |
__delitem__(self, key) | 定义删除容器中指定元素的行为,至关于 del self[key] |
__iter__(self) | 定义当迭代容器中的元素的行为 |
__reversed__(self) | 定义当被 reversed() 调用时的行为 |
__contains__(self, item) | 定义当使用成员测试运算符(in 或 not in)时的行为 |
上面基本上是Python中类的全部魔术方法了,下面针对一些重要的经常使用的方法进行说明。ssh
方法 | 意义 |
---|---|
__dir__() | 返回类或者对象的全部成员的名称列表 dir()函数操做实例调用的就是__dir__() |
当dir(obj)时,obj的__dir__()方法被调用,若是当前实例不存在该方法,则按照mro开始查找,若是父类都没有定义,那么最终会找到object.__dir__()方法,该方法会最大程度的收集属性信息。函数
class A: def __dir__(self): return 'ab' class B(A): pass print(dir(A)) # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] print(dir(B())) # ['a', 'b']
dir(obj)对于不一样类型的对象obj具备不一样的行为:性能
必须可迭代
)组成列表返回
。globals()
结果相同)locals()
结果相同)在方法中,返回本地做用域的变量名(和locals()
结果相同)测试
locals()运行在全局时,结果和globals()相同设计
方法 | 含义 |
---|---|
__new__ | 实例化一个对象 该方法须要返回一个值,若是该值不是cls的实现,则不会调用__init__ 该方法永远都是静态方法 |
class A: def __new__(cls, *args, **kwargs): print(cls) # <class '__main__.A'> print(args) # ('daxin',) print(kwargs) # {'age': 20} def __init__(self, name, age): self.name = name self.age = age daxin = A('daxin', age=20) print(daxin.name) # 'NoneType' object has no attribute 'name'
分析:code
class A(object): def __new__(cls, *args, **kwargs): print(cls) # <class '__main__.A'> print(args) # ('daxin',) print(kwargs) # {'age': 20} return super().__new__(cls) # 只须要传递cls便可 def __init__(self, name, age): self.name = name self.age = age daxin = A('daxin', 20) print(daxin) print(daxin.name) # 'NoneType' object has no attribute 'name' print(daxin.age) # 'NoneType' object has no attribute 'name'
注意:__new__方法不多用,即便建立了该方法,也会使用return super().__new__(cls),调用基类objct的__new__方法来建立实例并返回。除非使用元类编程。
方法 | 意义 |
---|---|
__hash__ | 内建函数hash()调用的返回值,返回一个整数。若是定义这个方法该类的实例就可hash。 |
__eq__ | 对应==操做符,判断2个对象是否相等,返回bool值 |
class A: def __hash__(self): return 123 # 返回值必须是数字 a = A() print(hash(a)) # 123
通常来讲提供__hash__方法是为了做为set或者dict的key的。可是key是不重复的,因此怎么去重呢?
前面咱们知道,set类型是不容许有重复数据的,那么它是怎么去重的呢?咱们说每一个元素的hash值都不相同,是经过hash值来去重的吗?
class A: def __init__(self, name): self.name = name def __hash__(self): return 123 # 返回值必须是数字 def __repr__(self): # 为了打印方便,这里使用repr定义类型输出格式 return '<{}>'.format(self.name) a = A('daxin') b = A('daxin') s = {a,b} print(s) # {<daxin>, <daxin>}
hash值相同的状况下,从结果看并无去重。因此,去重,并非只看hash值,还须要使用__eq__来判断2个对象是否相等。hash值相等,只是说明hash冲突,并不能说明两个对象是相等的。判断内容是否相等通常使用的是 == , 使用 == 进行比较时,就会触发对象的__eq__方法。因此咱们来测试一下。
class A: def __init__(self, name): self.name = name def __hash__(self): return 123 # 返回值必须是数字 def __repr__(self): # 为了打印方便,这里使用repr定义类型输出格式 return '<{} {}>'.format(self.name, id(self)) def __eq__(self, other): # self是实例自己,other就是等式右边的对象 return True # 衡返回True,就表示只要进行比较就相等 a = A('daxin') b = A('daxin') print(a,b) # <daxin 2621275933832> <daxin 2621275933888> print({a, b}) # {<daxin 2621275933832>}
因此:去重的条件,首先判断的是hash值,hash值相同的状况下,判断内容是否相等,确认是不是同一个元素。因此__eq__方法就很重要了。
def __eq__(self, other): return id(self) == id(other)
注意:
若是要去重,还须要配合__hash__方法,才能够变为可hash对象。
判断是不是不可hash对象,可使用:isinstance(a, collection.Hashable),False表示不可hash。
想一下为何list对象不可hash? 来看一下List的原码
... __hash__ = None ...
在list内部,它把__hash__属性置为None了,学到一招,因此若是一个对象,不能被hash,那就把它置为None把。
设计二维坐标类Ponit, 使其成为可hash类型,并比较2个坐标的实例,是否相等?
class Point: def __init__(self, x, y): self.x = x self.y = y def __hash__(self): return hash(self.x) + hash(self.y) # 使用+拼接有很大概率相同,这里可使用 return hash((self.x, self.y)) def __eq__(self, other): return self.x == other.x and self.y == other.y # 若是id相同,那么就不须要再比较属性了,由于确定是同一个数据 # return self is other or (self.x == other.x and self.y == other.y) def __repr__(self): return '{} {} {}'.format(self.x, self.y, id(self)) a = Point(3, 4) b = Point(3, '4') print(set([a, b]))
方法 | 意义 |
---|---|
__bool__ | 内建函数bool(),或者被看成逻辑表达式时,调用这个__bool__函数的返回值。 若是没有定义__bool__函数,那么就会寻找__len__返回长度,非0为真。 若是__len__()也没有定义,那么全部实例都返回真 |
即:使用bool时,先判断__bool__,而后再判断__len__,不然True
class A: def __bool__(self): return False print(bool(A())) # False class A: def __len__(self): # 只有len return 1 print(bool(A())) # True
__len__的返回值必须为大于0的整数。
可视化就是指实例的表现形式,好比print的时候实例如何显示,被看成参数传递时又该如何显示
方法 | 意义 |
---|---|
__str__ | str、format、print函数调用,须要返回对象字符串表达式。 若是没有定义,就去调用__repr__方法,返回字符串表达。 若是__repr__没有定义,就直接返回对象的内存地址信息。 |
__repr__ | 内建函数repr()对一个对象获取字符串表达式。 调用__repr__方法返回字符串表达式,若是__repr__也没有定义,就直接返回object的定义的(就是内存地址) |
__bytes__ | bytes()函数调用,返回一个对象的bytes表达,即返回bytes对象。 |
class A: def __init__(self): self.name = 'daxin' def __str__(self): return '{} from str method'.format(self.name) # 必须返回字符串 daxin = A() print(daxin) # daxin from str method print([daxin,daxin]) # [<__main__.A object at 0x00000207946A8EB8>, <__main__.A object at 0x00000207946A8EB8>] class A: def __init__(self): self.name = 'daxin' def __repr__(self): return '{} from repr method'.format(self.name) # 必须返回字符串 daxin = A() print(daxin) # daxin from repr method print([daxin,daxin]) # [daxin from repr method, daxin from repr method] class A: def __init__(self): self.name = 'daxin' def __bytes__(self): return self.name.encode('utf-8') # 必须是一个bytes对象 daxin = A() print(bytes(daxin)) # b'daxin'
不能经过判断是否在引号来判断输出值的类型,类型判断要使用type和instance。
用于使咱们本身定义的类支持运算符的操做。好比加减乘除这类运算符,须要注意的是通常的操做数都为2个,符号左边的称之为左实例,而右边的成为右实例。
当使用左实例减右实例时,会触发左实例的__sub__方法(若是左实例不存在__sub__方法,则会执行右实例的__rsub__方法)
class A: def __init__(self,value): self.value = value def __sub__(self, other): return self.value - other.value class B: def __init__(self,value): self.value = value def __rsub__(self, other): return 100 a=A(10) b=B(5) print(a-b) # 执行a的__sub__方法,若是没有就执行b的__rsub__方法。
当使用-=(减等)触发的是__isub__方法了,若是没有定义__isub__方法,那么最后调用__sub__方法。
class A: def __init__(self,value): self.value = value def __isub__(self, other): return self.value - other.value a = A(13) b = A(5) a -= b print(a, type(a)) # 8 <class 'int'>
注意:
完成坐标轴设计,实现向量的相等判断,以及加法运算
class Ponit: def __init__(self, x, y): self.x = x self.y = y def __eq__(self, other): return True if id(self) == id(other) else (self.x == other.x and self.y == other.y) def __add__(self, other): # return self.x + other.x,self.y + other.y # 直接返回(2,4) return self.__class__(self.x + other.x,self.y + other.y) # 返回一个新的实例 def __iadd__(self, other): self.x = self.x + other.x self.y = self.y + other.y return self def __str__(self): return '<{},{}>'.format(self.x,self.y) a = Ponit(1,2) b = Ponit(1,2) print(a==b) # True a += b # 调用a的__iadd__方法,原地修改 print(a) # <2,4> c = a + b # 调用a的__add__方法,能够返回结果,也能够返回一个新的对象,看本身需求 print(c) # <3,6> # 注意 a = Point(1,2) b = Point(3,4) c = Point(5,6) d = a + b + c # 之因此能够这样一直加,是由于__add__方法,返回了一个新的实例。执行+发时,调用的就是实例的__add__方法 print(d)
当咱们使用面向对象定义的类,须要大量的运算时,可使用这种运算符重载的方式编写,由于这中运算符是数学上最多见的表达方式。上面的例子就实现了Point类的二元操做,从新定义了Point + Point 甚至 Point + Point + Ponit。
int类,几乎实现了全部操做符。
__lt__,__le__,__eq__,__ne__,__gt__,__ge__等是大小比较经常使用的方法,可是所有写完比较麻烦,使用functools模块提供的total_ordering装饰器就是能够简化代码
可是要求__eq__必须实现,其余方法__lt__,__le__,__gt__,__ge__,实现其一。
import functools @functools.total_ordering class Person: def __init__(self,name,age): self.name = name self.age =age def __eq__(self, other): return self.age == other.age def __gt__(self, other): return self.age > other.age daxin = Person('daxin',99) dachenzi = Person('dachenzi',66) print(daxin == dachenzi) # False print(daxin < dachenzi) # False print(daxin > dachenzi) # True print(daxin >= dachenzi) # True print(daxin <= dachenzi) # False print(daxin != dachenzi) # True
虽然简化了不少代码,可是通常来讲实现等于或者小于方法也就够了,其余的能够不实现,这个装饰器只是看着很美好,且可能会带来性能问题,建议须要用到什么方法就本身建立,少用这个装饰器。
class Person: def __init__(self,name,age): self.name = name self.age =age def __eq__(self, other): return self.age == other.age def __gt__(self, other): return self.age > other.age def __ge__(self, other): return self.age >= other.age daxin = Person('daxin',99) dachenzi = Person('dachenzi',66) print(daxin == dachenzi) # False print(daxin < dachenzi) # False print(daxin > dachenzi) # True print(daxin >= dachenzi) # True print(daxin <= dachenzi) # False print(daxin != dachenzi) # True
为何这样写能够的呢?想一下:
仅仅添加了一个__ge__方法,就完成了需求,因此仍是建议本身写吧。
方法 | 意义 |
---|---|
__len__ | 内建函数len(),返回对象的长度(>=0的整数) 若是把对象看成容器类型看,就如同list或者dict。在bool()函数调用的时候,若是对象没有__bool__()方法,就会看__len__()方法是否存在,返回非0时,表示真 |
__iter__ | 迭代容器时,调用,返回一个新的迭代器对象 。 |
__contains__ | in成员操做符,没有实现,就用__iter__犯法遍历。 |
__getitem__ | 实现self[key]方式的访问,对于list对象,key为index,对于dict来讲,key为hashable,key不存在引起KeyError异常 |
__setitem__ | 和__getitem__的访问相似,是设置值时调用的方法。 |
__missing__ | 字典和其子类使用__getitm__()调用时,key不存在执行该方法。 |
class Contain: def __init__(self): self.items = [] def __iter__(self): # return (item for item in self.items) # 生成器是一个特殊的迭代器 return iter(self.items) # 或者直接包装新的迭代器 def __contains__(self, value): for item in self.items: if item == value: return True else: return False def __getitem__(self, index): return self.items[index] def __setitem__(self, key, value): self.items[key] = value def __add__(self, other): self.items.append(other) return self def __str__(self): return '{}'.format(id(self)) __repr__ = __str__ c = Contain() d = Contain() e = Contain() c + d + e print(c.items) print(c[1]) c[1] = e for i in c: print(i)
Python中一切皆对象,函数也不例外,函数名加上(),就表示调用函数对象的__call__方法
def func(): print(func.__name__,func.__module__) func() # func __main__ 等于 func().__call__()
方法 | 含义 |
---|---|
__call__ | 类中定义一个该方法,实例就能够像函数同样调用 |
class Call: def __call__(self, *args, **kwargs): return 'hello world' a = Call() print(a()) # 调用c.__call__方法
定义一个斐波那契数列的类,方便调用计算第N项,增长迭代方法,返回容器长度,支持索引方法.
class Fib: def __init__(self): self.items = [0,1,1] def __iter__(self): return iter(self.items) def __len__(self): return len(self.items) def __getitem__(self, index): length = len(self.items) # 3 4 if index < 0: raise KeyError if index >= length: for i in range(length,index+1): # 3,5 self.items.append(self.items[i-1] + self.items[i-2]) return self.items[index] def __call__(self, index): return self.__getitem__(index) # return self[index] # 这里调用直接经过key的方式访问元素,其实仍是会调用__getitem__方法。self[index] == self.__getitem__(index) f = Fib() print(f[101]) print(f(101))