当前位置: 移动技术网 > IT编程>脚本编程>Python > 草根学Python(十)Python 的 Magic Method

草根学Python(十)Python 的 Magic Method

2018年03月29日  | 移动技术网IT编程  | 我要评论

音速启动怎么卸载,qq情侣文字头像,茂名学院论坛

草根学Python(十)Python 的 Magic Method。

一、Python 的 Magic Method

在 Python 中,所有以 “__” 双下划线包起来的方法,都统称为”魔术方法”。比如我们接触最多的 __init__ 。魔术方法有什么作用呢?

使用这些魔术方法,我们可以构造出优美的代码,将复杂的逻辑封装成简单的方法。

那么一个类中有哪些魔术方法呢?

我们可以使用 Python 内置的方法 dir() 来列出类中所有的魔术方法.示例如下:

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

class User(object):
    pass


if __name__ == '__main__':
    print(dir(User()))

输出的结果:

可以看到,一个类的魔术方法还是挺多的,截图也没有截全,不过我们只需要了解一些常见和常用的魔术方法就好了。

二、构造(__new__)和初始化(__init__)

通过上一篇的内容,我们已经知道定义一个类时,我们经常会通过 __init__(self) 的方法在实例化对象的时候,对属性进行设置。比如下面的例子:

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

class User(object):
    def __init__(self, name, age):
        self.name = name;
        self.age = age;

user=User('两点水',23)

实际上,创建一个类的过程是分为两步的,一步是创建类的对象,还有一步就是对类进行初始化。__new__ 是用来创建类并返回这个类的实例, 而__init__ 只是将传入的参数来初始化该实例.__new__ 在创建一个实例的过程中必定会被调用,但 __init__ 就不一定,比如通过pickle.load 的方式反序列化一个实例时就不会调用 __init__ 方法。

def __new__(cls) 是在 def __init__(self) 方法之前调用的,作用是返回一个实例对象。还有一点需要注意的是:__new__ 方法总是需要返回该类的一个实例,而 __init__ 不能返回除了 None 的任何值

具体的示例:

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

class User(object):
    def __new__(cls, *args, **kwargs):
        # 打印 __new__方法中的相关信息
        print('调用了 def __new__ 方法')
        print(args)
        # 最后返回父类的方法
        return super(User, cls).__new__(cls)

    def __init__(self, name, age):
        print('调用了 def __init__ 方法')
        self.name = name
        self.age = age


if __name__ == '__main__':
    usr = User('两点水', 23)

看看输出的结果:

def __setattr__(self, name, value):
    self.name = value
    # 每当属性被赋值的时候, ``__setattr__()`` 会被调用,这样就造成了递归调用。
    # 这意味这会调用 ``self.__setattr__('name', value)`` ,每次方法会调用自己。这样会造成程序崩溃。

def __setattr__(self, name, value):
    # 给类中的属性名分配值
    self.__dict__[name] = value  
    # 定制特有属性

上面方法的调用具体示例如下:

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

class User(object):
    def __getattr__(self, name):
        print('调用了 __getattr__ 方法')
        return super(User, self).__getattr__(name)

    def __setattr__(self, name, value):
        print('调用了 __setattr__ 方法')
        return super(User, self).__setattr__(name, value)

    def __delattr__(self, name):
        print('调用了 __delattr__ 方法')
        return super(User, self).__delattr__(name)

    def __getattribute__(self, name):
        print('调用了 __getattribute__ 方法')
        return super(User, self).__getattribute__(name)


if __name__ == '__main__':
    user = User()
    # 设置属性值,会调用 __setattr__
    user.attr1 = True
    # 属性存在,只有__getattribute__调用
    user.attr1
    try:
        # 属性不存在, 先调用__getattribute__, 后调用__getattr__
        user.attr2
    except AttributeError:
        pass
    # __delattr__调用
    del user.attr1

输出的结果:

#!/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)

输出的结果如下:

#!/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) 实现了乘法运算
__floorp__(self, other) 实现了 // 运算符
___p__(self, other) 实现了/运算符. 该方法在 Python3 中废弃. 原因是 Python3 中,pision 默认就是 true pision
__truep__(self, other) 实现了 true pision. 只有你声明了 from __future__ import pision 该方法才会生效
__mod__(self, other) 实现了 % 运算符, 取余运算
__pmod__(self, other) 实现了 pmod() 內建函数
__pow__(self, other) 实现了 ** 操作. N 次方操作
__lshift__(self, other) 实现了位操作 <<
__rshift__(self, other) 实现了位操作 >>
__and__(self, other) 实现了位操作 &
__or__(self, other) 实现了位操作 `
__xor__(self, other) 实现了位操作 ^

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网