25 - 面向对象高级-魔术方法基础

1 魔术方法

  在Python中以两个下划线开头和结尾的方法,比如:__init__、__str__、__doc__、__new__等,被称为"魔术方法"(Magic methods)。魔术方法在类或对象的某些事件出发后会自动执行,如果希望根据自己的程序定制自己特殊功能的类,那么就需要对这些方法进行重写。

Python 将所有以 __(两个下划线)开头和结尾的类方法保留为魔术方法。所以在定义类方法时,除了上述魔术方法,建议不要以 __ 为前缀。

2 类的魔术方法

我们将不同类型的魔术方法进行归类,那么会分为以下几类。

2.1 基本的魔法方法和常用属性

魔术方法 含义
__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__ 类或实例的属性,可写的字典

2.2 有关属性

魔术方法 含义
__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) 定义当描述符的值被删除时的行为

2.3 比较操作符

魔术方法 含义
__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)

2.4 算数运算符

魔术方法 含义
__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) 定义按位或操作的行为:

2.5 反运算

魔术方法 含义
__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) (与上方相同,当左操作数不支持相应的操作时被调用)

2.6 增量赋值运算

魔术方法 含义
__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) 定义赋值按位或操作的行为:

2.7 一元操作符

魔术方法 含义
__pos__(self) 定义正号的行为:+x
__neg__(self) 定义负号的行为:-x
__abs__(self) 定义当被 abs() 调用时的行为
__invert__(self) 定义按位求反的行为:~x

2.8 类型转换

魔术方法 含义
__complex__(self) 定义当被 complex() 调用时的行为(需要返回恰当的值)
__int__(self) 定义当被 int() 调用时的行为(需要返回恰当的值)
__float__(self) 定义当被 float() 调用时的行为(需要返回恰当的值)
__round__(self[, n]) 定义当被 round() 调用时的行为(需要返回恰当的值)
__index__(self) 1. 当对象是被应用在切片表达式中时,实现整形强制转换
2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__
3. 如果 __index__ 被定义,则 __int__ 也需要被定义,且返回相同的值

2.9 上下文管理(with 语句)

魔术方法 含义
__enter__(self) 1. 定义当使用 with 语句时的初始化行为
2. __enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定
__exit__(self, exc_type, exc_value, traceback) 1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么
2. 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作

2.10 容器类型

魔术方法 含义
__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)时的行为

3 常用方法

上面基本上是Python中类的所有魔术方法了,下面针对一些重要的常用的方法进行说明。

3.1 查看属性

方法 意义
__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具有不同的行为:

  1. 如果对象是模块对象,返回的列表包含模块的属性和变量名
  2. 如果对象是类型或者说是类对象,返回的列表包含类的属性名,及它的祖先类的属性名
  3. 如果是类的实例
    • 有__dir__方法,则返回__dir__方法的返回值(必须可迭代)
    • 没有__dir__方法,则尽可能的收集实例的属性名、类的属性和祖先类的属性名,组成列表返回
  4. 如果dir没有参数,返回列表包含的内容也不同。
    • 在模块中,返回模块的属性和变量名(和globals()结果相同)
    • 在函数中,返回本地作用域的变量名(和locals()结果相同)
    • 在方法中,返回本地作用域的变量名(和locals()结果相同)

      locals()运行在全局时,结果和globals()相同

3.2 实例化

方法 含义
__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'

分析:

  1. 实例化时执行__new__函数,进行实例化操作。
  2. 前面学的__init__函数,我们知道它必须返回None,所以构建好的实例应该是在__new__函数中返回的。
  3. 顺序:__new__构建实例,调用__init__函数进行初始化,然后由__new__函数返回实例。
  4. 我们不知道实例到底是如何实例化的,所以这里可以使用super函数
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__方法来创建实例并返回。除非使用元类编程。

3.3 hash相关

方法 意义
__hash__ 内建函数hash()调用的返回值,返回一个整数。如果定义这个方法该类的实例就可hash。
__eq__ 对应==操作符,判断2个对象是否相等,返回bool值
class A:
    def __hash__(self):
        return 123   # 返回值必须是数字

a = A()
print(hash(a))  # 123

一般来说提供__hash__方法是为了作为set或者dict的key的。但是key是不重复的,所以怎么去重呢?

3.3.1 hash相同能否去重

前面我们知道,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>}

3.3.2 比较内容是否相同

        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)

注意:

  1. __eq__:返回值必须是一个bool类型。
  2. 只写__eq__方法,那么当前实例就无法可hash了,所以就无法去重了。
  3. 如果要去重,还需要配合__hash__方法,才可以变为可hash对象。

    判断是否是不可hash对象,可以使用:isinstance(a, collection.Hashable),False表示不可hash。

想一下为什么list对象不可hash? 来看一下List的原码

...
    __hash__ = None
...

在list内部,它把__hash__属性置为None了,学到一招,所以如果一个对象,不能被hash,那就把它置为None把。

3.3.3 坐标轴小例子

设计二维坐标类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]))

3.4 bool类型

方法 意义
__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的整数。

3.5 可视化

可视化就是指实例的表现形式,比如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。

3.6 运算符重载

        用于使我们自己定义的类支持运算符的操作。比如加减乘除这类运算符,需要注意的是一般的操作数都为2个,符号左边的称之为左实例,而右边的成为右实例。

3.6.1 实现两个实例相减

        当使用左实例减右实例时,会触发左实例的__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'>

注意:

  1. 使用了减等之后,我们发现连实例的a的类型都被修改了。
  2. __isub__的返回值会返回给a。
  3. 仅仅在数字计算时可以使用这种方式,如果是实例相减,一般会in-place修改(就地修改)

3.6.2 坐标轴小例子

完成坐标轴设计,实现向量的相等判断,以及加法运算

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)

3.6.3 应用场景

        当我们使用面向对象定义的类,需要大量的运算时,可以使用这种运算符重载的方式编写,因为这中运算符是数学上最常见的表达方式。上面的例子就实现了Point类的二元操作,重新定义了Point + Point 甚至 Point + Point + Ponit。

int类,几乎实现了所有操作符。

3.6.4 total_ordering装饰器

        __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

为什么这样写可以的呢?想一下:

  1. 通过 __eq__ 就可以推断出 __ne__
  2. 通过 __gt__ 就可以推断出 __lt__
  3. 通过 __ge__ 就可以推断出 __le__

仅仅添加了一个__ge__方法,就完成了需求,所以还是建议自己写吧。

3.7 容器相关方法

方法 意义
__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)

3.8 可调用对象

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))

猜你喜欢

转载自www.cnblogs.com/dachenzi/p/10505482.html