python基础知识(24)Magic Method 2

本文详细介绍了Python中的描述器如何重写属性访问控制,自定义不可变和可变容器的实现,以及运算符相关魔术方法的使用,如__get__,__set__,__delitem__和比较/算术运算符的定义。
摘要由CSDN通过智能技术生成

 

四、对象的描述器

一般来说,一个描述器是一个有“绑定行为”的对象属性 (object attribute),它的访问控制被描述器协议方法重写。

这些方法是 __get__()__set__() , 和 __delete__() 。

有这些方法的对象叫做描述器。

默认对属性的访问控制是从对象的字典里面 (__dict__) 中获取 (get) , 设置 (set) 和删除 (delete) 。

举例来说, a.x 的查找顺序是, a.__dict__['x'] , 然后 type(a).__dict__['x'] , 然后找 type(a) 的父类 ( 不包括元类 (metaclass) ).如果查找到的值是一个描述器, Python 就会调用描述器的方法来重写默认的控制行为。

这个重写发生在这个查找环节的哪里取决于定义了哪个描述器方法。

注意, 只有在新式类中时描述器才会起作用。在之前的篇节中已经提到新式类和旧式类的,有兴趣可以查看之前的篇节来看看,至于新式类最大的特点就是所有类都继承自 type 或者 object 的类。

在面向对象编程时,如果一个类的属性有相互依赖的关系时,使用描述器来编写代码可以很巧妙的组织逻辑。在 Django 的 ORM 中,models.Model 中的 InterField 等字段, 就是通过描述器来实现功能的。

我们先看下下面的例子:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

class User(object):
    def __init__(self, name='两点水', sex='男'):
        self.sex = sex
        self.name = name

    def __get__(self, obj, objtype):
        print('获取 name 值')
        return self.name

    def __set__(self, obj, val):
        print('设置 name 值')
        self.name = val


class MyClass(object):
    x = User('两点水', '男')
    y = 5


if __name__ == '__main__':
    m = MyClass()
    print(m.x)

    print('\n')

    m.x = '三点水'
    print(m.x)

    print('\n')

    print(m.x)

    print('\n')

    print(m.y)

输出的结果如下:

获取 name 值
两点水


设置 name 值
获取 name 值
三点水


获取 name 值
三点水


5

通过这个例子,可以很好的观察到这 __get__() 和 __set__() 这些方法的调用。

再看一个经典的例子

我们知道,距离既可以用单位"米"表示,也可以用单位"英尺"表示。 现在我们定义一个类来表示距离,它有两个属性: 米和英尺。

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-


class Meter(object):
    def __init__(self, value=0.0):
        self.value = float(value)

    def __get__(self, instance, owner):
        return self.value

    def __set__(self, instance, value):
        self.value = float(value)


class Foot(object):
    def __get__(self, instance, owner):
        return instance.meter * 3.2808

    def __set__(self, instance, value):
        instance.meter = float(value) / 3.2808


class Distance(object):
    meter = Meter()
    foot = Foot()


if __name__ == '__main__':
    d = Distance()
    print(d.meter, d.foot)
    d.meter = 1
    print(d.meter, d.foot)
    d.meter = 2
    print(d.meter, d.foot)

输出的结果:

0.0 0.0
1.0 3.2808
2.0 6.5616

在上面例子中,在还没有对 Distance 的实例赋值前, 我们认为 meter 和 foot 应该是各自类的实例对象, 但是输出却是数值。这是因为 __get__ 发挥了作用.

我们只是修改了 meter ,并且将其赋值成为 int ,但 foot 也修改了。这是 __set__ 发挥了作用.

描述器对象 (Meter、Foot) 不能独立存在, 它需要被另一个所有者类 (Distance) 所持有。描述器对象可以访问到其拥有者实例的属性,比如例子中 Foot 的 instance.meter 。

 

五、自定义容器(Container)

经过之前编章的介绍,我们知道在 Python 中,常见的容器类型有: dict, tuple, list, string。其中也提到过可容器和不可变容器的概念。其中 tuple, string 是不可变容器,dict, list 是可变容器。

可变容器和不可变容器的区别在于,不可变容器一旦赋值后,不可对其中的某个元素进行修改。当然具体的介绍,可以看回之前的文章,有图文介绍。

那么这里先提出一个问题,这些数据结构就够我们开发使用吗?

不够的时候,或者说有些特殊的需求不能单单只使用这些基本的容器解决的时候,该怎么办呢?

这个时候就需要自定义容器了,那么具体我们该怎么做呢?

功能说明
自定义不可变容器类型需要定义 __len__ 和 __getitem__ 方法
自定义可变类型容器在不可变容器类型的基础上增加定义 __setitem__ 和 __delitem__
自定义的数据类型需要迭代需定义 __iter__
返回自定义容器的长度需实现 __len__(self)
自定义容器可以调用 self[key] ,如果 key 类型错误,抛出TypeError ,如果没法返回key对应的数值时,该方法应该抛出ValueError需要实现 __getitem__(self, key)
当执行 self[key] = value 时调用是 __setitem__(self, key, value)这个方法
当执行 del self[key] 方法其实调用的方法是 __delitem__(self, key)
当你想你的容器可以执行 for x in container: 或者使用 iter(container) 时需要实现 __iter__(self) ,该方法返回的是一个迭代器

来看一下使用上面魔术方法实现 Haskell 语言中的一个数据结构:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

class FunctionalList:
    ''' 实现了内置类型list的功能,并丰富了一些其他方法: head, tail, init, last, drop, take'''

    def __init__(self, values=None):
        if values is None:
            self.values = []
        else:
            self.values = values

    def __len__(self):
        return len(self.values)

    def __getitem__(self, key):
        return self.values[key]

    def __setitem__(self, key, value):
        self.values[key] = value

    def __delitem__(self, key):
        del self.values[key]

    def __iter__(self):
        return iter(self.values)

    def __reversed__(self):
        return FunctionalList(reversed(self.values))

    def append(self, value):
        self.values.append(value)

    def head(self):
        # 获取第一个元素
        return self.values[0]

    def tail(self):
        # 获取第一个元素之后的所有元素
        return self.values[1:]

    def init(self):
        # 获取最后一个元素之前的所有元素
        return self.values[:-1]

    def last(self):
        # 获取最后一个元素
        return self.values[-1]

    def drop(self, n):
        # 获取所有元素,除了前N个
        return self.values[n:]

    def take(self, n):
        # 获取前N个元素
        return self.values[:n]

 

六、运算符相关的魔术方法

运算符相关的魔术方法实在太多了,j就大概列举下面两类:

1、比较运算符

魔术方法说明
__cmp__(self, other)如果该方法返回负数,说明 self < other; 返回正数,说明 self > other; 返回 0 说明 self == other 。强烈不推荐来定义 __cmp__ , 取而代之, 最好分别定义 __lt____eq__ 等方法从而实现比较功能。 __cmp__ 在 Python3 中被废弃了。
__eq__(self, other)定义了比较操作符 == 的行为
__ne__(self, other)定义了比较操作符 != 的行为
__lt__(self, other)定义了比较操作符 < 的行为
__gt__(self, other)定义了比较操作符 > 的行为
__le__(self, other)定义了比较操作符 <= 的行为
__ge__(self, other)定义了比较操作符 >= 的行为

来看个简单的例子就能理解了:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

class Number(object):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        print('__eq__')
        return self.value == other.value

    def __ne__(self, other):
        print('__ne__')
        return self.value != other.value

    def __lt__(self, other):
        print('__lt__')
        return self.value < other.value

    def __gt__(self, other):
        print('__gt__')
        return self.value > other.value

    def __le__(self, other):
        print('__le__')
        return self.value <= other.value

    def __ge__(self, other):
        print('__ge__')
        return self.value >= other.value


if __name__ == '__main__':
    num1 = Number(2)
    num2 = Number(3)
    print('num1 == num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 != num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 < num2 ? --------> {} \n'.format(num1 < num2))
    print('num1 > num2 ? --------> {} \n'.format(num1 > num2))
    print('num1 <= num2 ? --------> {} \n'.format(num1 <= num2))
    print('num1 >= num2 ? --------> {} \n'.format(num1 >= num2))

输出的结果为:

__eq__
num1 == num2 ? --------> False

__eq__
num1 != num2 ? --------> False

__lt__
num1 < num2 ? --------> True

__gt__
num1 > num2 ? --------> False

__le__
num1 <= num2 ? --------> True

__ge__
num1 >= num2 ? --------> False

2、算术运算符

魔术方法说明
__add__(self, other)实现了加号运算
__sub__(self, other)实现了减号运算
__mul__(self, other)实现了乘法运算
__floordiv__(self, other)实现了 // 运算符
___div__(self, other)实现了/运算符. 该方法在 Python3 中废弃. 原因是 Python3 中,division 默认就是 true division
__truediv__(self, other)实现了 true division. 只有你声明了 from __future__ import division 该方法才会生效
__mod__(self, other)实现了 % 运算符, 取余运算
__divmod__(self, other)实现了 divmod() 內建函数
__pow__(self, other)实现了 ** 操作. N 次方操作
__lshift__(self, other)实现了位操作 <<
__rshift__(self, other)实现了位操作 >>
__and__(self, other)实现了位操作 &
__or__(self, other)实现了位操作 `
__xor__(self, other)实现了位操作 ^

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值