python魔术方法总结

获取属性

  • __ getattr __(self, name) 定义当用户试图获取一个不存在的属性时的行为
  • __ getattribute __(self, name) 定义当该类的属性被访问时的行为 (获取属性一定会走到这个方法,获取不到属性去访问 __ getattr __)
  • __ setattr __(self, name, value) 定义当一个属性被设置时的行为,小心递归调用
  • __ delattr __(self, name) 定义当一个属性被删除时的行为
  • __ dir __(self) 定义当 dir() 被调用时的行为
  • __ get __(self, instance, owner) 定义当描述符的值被取得时的行为
  • __ set __(self, instance, value) 定义当描述符的值被改变时的行为
  • __ delete __(self, instance) 定义当描述符的值被删除时的行为

实例对象属性寻找的顺序如下:

① 首先访问 getattribute() 魔法方法(隐含默认调用,无论何种情况,均会调用此方法)

② 去实例对象t中查找是否具备该属性: t.dict 中查找,每个类和实例对象都有一个 dict 的属性

③ 若在 t.dict 中找不到对应的属性, 则去该实例的类中寻找,即 t.class.dict

④ 若在实例的类中也招不到该属性,则去父类中寻找,即 t.class.bases.__dict__中寻找

⑤ 若以上均无法找到,则会调用 getattr 方法,执行内部的命令(若未重载 getattr 方法,则直接报错:AttributeError)

以上几个流程,即完成了属性的寻找。
注意: 第④步,一个类一旦重载了 getattribute() 方法,如果找不到属性,则必须要手动加入第④步,否则无法进入到 第⑤步 (getattr)的。可以用super解决


class Test:
    def __getattr__(self, name):
        print('__getattr__')

    def __getattribute__(self, name):
        print('__getattribute__')
        super().__getattribute__(name)

    def __setattr__(self, name, value):
        print('__setattr__')

    def __delattr__(self, name):
        print('__delattr__')


>>> t=Test()
>>> t.x
__getattribute__
__getattr__

某个类,只要是内部定义了方法 get, set, delete 中的一个或多个,就可以称为描述符


class Desc(object):

    def __get__(self, instance, owner):
        print("__get__...")
        print("self : \t\t", self)
        print("instance : \t", instance)
        print("owner : \t", owner)
        print('='*40, "\n")

    def __set__(self, instance, value):
        print('__set__...')
        print("self : \t\t", self)
        print("instance : \t", instance)
        print("value : \t", value)
        print('='*40, "\n")


class TestDesc(object):
    x = Desc()

#以下为测试代码
t = TestDesc()
t.x

#以下为输出信息:

__get__...
self :          <__main__.Desc object at 0x0000000002B0B828>
instance :      <__main__.TestDesc object at 0x0000000002B0BA20>
owner :      <class '__main__.TestDesc'>

注意: 查找顺序问题:当Python解释器发现实例对象的字典中,有与描述符同名的属性时,描述符优先,会覆盖掉实例属性。

属性查询优先级小总结

    ① getattribute(), 无条件调用

    ② 数据描述符:由 ① 触发调用 (若人为的重载了该 getattribute() 方法,可能会导致无法调用描述符)

    ③ 实例对象的字典(若与描述符对象同名,会被覆盖哦)

    ④ 类的字典

    ⑤ 非数据描述符

    ⑥ 父类的字典

    ⑦ getattr() 方法

基本魔术方法

  • __ new __(cls[, ...])

  • new 是在一个对象实例化的时候所调用的第一个方法

  • 它的第一个参数是这个类,其他的参数是用来直接传递给 init 方法

  • new 决定是否要使用该 init 方法,因为 new 可以调用其他类的构造方法或者直接返回别的实例对象来作为本类的实例,如果 new 没有返回实例对象,则 init 不会被调用

  • 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() 调用时的行为

切片和索引

class Student(object):
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores

    # 索引的是学生的成绩
    def __getitem__(self, index):  # 1).索引值的获取
        print(index, type(index))
        return self.scores[index]


    def __setitem__(self, key, value): # 2). 索引值的重新赋值
        self.scores[key] = value


    def __delitem__(self, key):  # 3). 删除索引值
        del  self.scores[key]


    def __mul__(self, other):  # 4). 实现*的效果, 具体返回什么取决于代码的业务需求
        """对于学生的每一门成绩乘3"""
        return  [i*other for i in self.scores]


    def __add__(self, other):  # 5). 连接的时候必须是同一种数据;类型
        # 将两个学生的成绩拼接起来
        return  self.scores + other.scores

    def __contains__(self, item): # 6). 判断某个元素是否存在于这个对象中?
        return  item in self.scores


    def __iter__(self):   # 7). 迭代, 使得该对象可以实现for循环
        # 将列表转换为迭代的类型, 可以for循环, 一定要返回iter类型的数据;
        return  iter(self.scores)

    def __lt__(self, other):  # 8). 比较两个对象的大小;
        return  (sum(self.scores)/3) < (sum(other.scores)/3)

liming = Student('liming', [100, 89, 100])

# # 1).索引值的获取
# print(liming[0])
# print(liming[1])
# print(liming[2])
#
# # 2).索引值的重新赋值
# liming[0] = 90
# print(liming[0])
#
# # 3). 删除索引值
# del liming[0]
# # print(liming[:2])
#


# # 1).切片值的获取
# print(liming[:2])
# print(liming[-2:])
#
#
# # 2).切片值的重新赋值
# liming[:2] = [10, 10]
# print(liming.scores)
#
# # 3). 删除切片值
# del liming[:2]
# print(liming.scores)

#
# # 4). 判断是否可以重复?
# print(liming * 3)
#
# # 5). 连接?
xiaohong = Student('小红', [100, 90, 90])
# print(xiaohong + liming)
#
#
#
# # 6). 成员操作符? 判断是否在对象里面存在?
# print(100 in xiaohong)
# print(101 in xiaohong)
# print(101 not in xiaohong)
#
#


# # 7). 实现for循环?
# for item in liming:
#     print(item)


# 8). 比较对象的大小?
# print(liming >  xiaohong)
print(liming <  xiaohong)

比较操作符

  • __ 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)
  • 当对象是被应用在切片表达式中时,实现整形强制转换
  • 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 index
  • 如果 index 被定义,则 int 也需要被定义,且返回相同的值

上下文管理(with 语句)

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

容器类型

  • 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)时的行为
posted @ 2020-12-09 15:49  rm-rf*  阅读(118)  评论(0编辑  收藏  举报