流畅的Python(二十一)-类元编程

一、核心要义

1.类元编程时指在运行时创建或定制类的技艺

2.类是一等对象,因此任何时候都可以使用函数新建类,而无需使用class关键字

3.类装饰器也是函数,不过能够审查、修改,甚至把被装饰的类替换为其它类。

4.元类(type类的子类)类编程最高级的工具:使用元类可以创建具有某种特质的全新类种,例如我们见过的抽象基类。

5.所有类都是type类的实例,但只有元类既是type类的实例,也是其子类。

温馨提示:除非开发框架,否则不要编写元类。

二、代码示例

1、类工厂函数

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/17 18:24
# @Author  : Maple
# @File    : 01-类工厂函数.py
# @Software: PyCharm


def record_factory(cls_name,field_names):

    try:
        field_names = field_names.replace(',',' ').split(' ')
    except AttributeError:
        pass
    field_names = tuple(field_names)

    def __init__(self,*args,**kwargs):
        # 属性键值对:处理元组参数
        attrs = dict(zip(self.__slots__,args))
        # 属性键值对:处理key-value参数
        attrs.update(**kwargs)

        for name,value in attrs.items():
            # 为实例对象设置属性
            setattr(self,name,value)

    def __iter__(self):
        for name in self.__slots__:
            yield getattr(self,name)

    def __repr__(self):
        # 注意zip(self.__slots__,self)中后面一个self,其实是一个元组(各个属性对应的值)遍历,因为实例实现了__iter__方法,
        values = ', '.join('{}={!r}'.format(*i) for i in zip(self.__slots__,self))
        return '{}({})'.format(self.__class__.name,values)

    # 组件类属性字典
    cls_attrs = dict(__slots__ = field_names,
                     __init__ = __init__,
                     __iter__ = __iter__,
                     __repr__ = __repr__)

    # 返回一个类
    return type(cls_name,(object,),cls_attrs)


if __name__ == '__main__':
    # 1.创建一个Dog的类(也是一个对象-万物皆对象)
    Dog1 = record_factory('Dog','name,age,gender')
    d1 = Dog1('旺财',18,'母') # <member 'name' of 'Dog' objects>(name='旺财', age=18, gender='母')
    print(d1) # <member 'name' of 'Dog' objects>(name='旺财', age=18, gender='母')

    # 2.创建另外一个Dog类(也是一个对象-万物皆对象)
    Dog2 = record_factory('Dog','name,age,gender')
    d2 = Dog2('阿来', 20, '公')
    print(d2) # <member 'name' of 'Dog' objects>(name='阿来', age=20, gender='公')

    # 3.Dog2和Dog1不是同一个对象
    print(id(Dog2) == id(Dog1)) #False
    print(Dog1 == Dog2) # False


2、定制描述符的类装饰器

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/17 20:54
# @Author  : Maple
# @File    : 02-定制描述符的类装饰器.py
# @Software: PyCharm


import abc


def entity(cls):
    #keyde的值类似:weight
    #attr的值类似:<__main__.Quantity object at 0x00000262A8BBA3A0>,是一个描述符对象
    for key,attr in cls.__dict__.items():
        if isinstance(attr,Validated):
            # type_name类似:Quantity
            type_name = type(attr).__name__
            attr.storage_name = '_{}#{}'.format(type_name,key)

    return cls

class AutoStorage:
    __counter = 0

    def __init__(self):
        cls = self.__class__
        prefix = cls.__name__
        index = cls.__counter
        self.storage_name = '_{}#{}'.format(prefix,index)
        cls.__counter += 1

    def __get__(self, instance, owner):
        if instance is None:
            return self
        else:
            return getattr(instance,self.storage_name)

    def __set__(self, instance, value):
        setattr(instance,self.storage_name,value)


class Validated(abc.ABC,AutoStorage):
    def __set__(self, instance, value):
        value = self.validate(instance,value)
        super().__set__(instance,value)


    @abc.abstractmethod
    def validate(self,instance,value):
        """retuen validated value or raise ValueError"""


class Quantity(Validated):
    """a number greater than zero"""
    def validate(self,instance,value):
        if value > 0:
            return value
        else:
            raise ValueError('value must be >0')

class NonBlank(Validated):
    """a string with at least one non-space character"""
    def validate(self,instance,value):
        value = value.strip()
        if len(value) == 0:
            raise ValueError('value can not be empty or blank')
        else:
            return value


# 用entity装饰LineItem类
# 默认的属性名变成类似_Quantity#weight和_Quantity#price,_NonBlank#price
@entity
class LineItem:

    description = NonBlank()
    weight = Quantity()
    price = Quantity()

    def __init__(self,description,weight,price):
        self.description = description
        self.weight = weight
        self.price = price

    def subtotal(self):
        return self.weight * self.price

    def __repr__(self):

        return 'LineItem =({},{},{})'.format(self.description,self.weight,self.price)

if __name__ == '__main__':
    
    com = LineItem('Computer', 10, 1000)
    # 查看实例属性的名
    print(com.__dict__) # {'_NonBlank#description': 'Computer', '_Quantity#weight': 10, '_Quantity#price': 1000}

3、导入时和运行时比较

(1) evalsupport.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/18 21:03
# @Author  : Maple
# @File    : evalsupport.py
# @Software: PyCharm


print('<[100]> evalsupport module start')

def deco_alpha(cls):
    print('<[200]> deco_alpha')

    def innner_1(self):
        print('<[300]> deco_alpha:inner_1')

    cls.method_y  = innner_1
    return  cls

# 注意MetaAleph继承type,因此它是一个元类
class MetaAleph(type):
    print('<[400]> MetaAleph body')

    def __init__(cls,name,bases,dict):
        print('<500> MetaAleph.__init__')

        def inner_2(self):
            print('<600> MetaAleph.__init__:inner_2')

        cls.method_z = inner_2

print('<[700]> evalsupport module end')

(2) evaltime.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/18 20:51
# @Author  : Maple
# @File    : evaltime.py
# @Software: PyCharm


from evalsupport import deco_alpha

print('<[1]> evaltime module start')

class ClassOne(object):
    print('<[2]> ClassOne body')

    def __init__(self):
        print('<[3]> ClassOne.__init__')

    def __del__(self):
        print('<[4]> ClassOne.__del__')

    def method_x(self):
        print('<[5]> ClassOne.method_x')

    class ClassTwo(object):
        print('<[6]> ClassTwo body')

@deco_alpha
class ClassThree(object):
    print('<[7]> ClassThree body')

    def method_y(self):
        print('<[8]> ClassThree.method_y')

class ClassFour(object):
    print('<[9]> ClassFour body')

    def method_y(self):
        print('<[10]> ClassFour.method_y')


if __name__ == '__main__':
    print('<[11]> ClassOne tests', 30 * '.')
    one = ClassOne()
    one.method_x()

    print('<[12]> ClassThree tests', 30 * '.')
    three = ClassThree()
    three.method_y()

    print('<[13]> ClassFour tests', 30 * '.')
    four = ClassFour()
    four.method_y()


print('<[14]> evaltime module end')

(3) 导入时

① 打开Python Console窗口

② 执行import evaltime

Tips:此时evaltime.py文件的name = __main__部分并没有执行

(4) 运行时

注意:在Teminal窗口执行

重点关注部分:

No1:因为@deco_alpha装饰器修改了ClassThree的method_y方法为inner_1,因此当three调用method_1的时候,inner_1方法会调用,并输出<[300]> deco_alpha:inner_1

No2: 虽然ClassFour继承了ClassThree,但是@deco_alpha装饰器并不会作用在ClassFour上,因此four.method_y的时候,并没有输出<[300]> deco_alpha:inner_1

4、元类的运行顺序

(1)evaltime_meta.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/18 21:03
# @Author  : Maple
# @File    : evalsupport.py
# @Software: PyCharm


print('<[100]> evalsupport module start')

def deco_alpha(cls):
    print('<[200]> deco_alpha')

    def innner_1(self):
        print('<[300]> deco_alpha:inner_1')

    cls.method_y  = innner_1
    return  cls

# 注意MetaAleph继承type,因此它是一个元类
class MetaAleph(type):
    print('<[400]> MetaAleph body')

    def __init__(cls,name,bases,dict):
        print('<500> MetaAleph.__init__')

        def inner_2(self):
            print('<600> MetaAleph.__init__:inner_2')

        cls.method_z = inner_2

print('<[700]> evalsupport module end')

(2)导入时

重点说明

    1. 创建ClassFive时,调用了MetaAleph.__init__方法

    2. 创建ClassFive的子类ClassSix时,同样会调用MetaAleph.__init__方法

(3)运行时

要点:元类会改变被修饰的类的一些特性

和装饰器@deco_alpha的区别是,MetaAleph的作用在ClassFive的子类ClassSix种仍然起作用。

5、定制描述符的元类

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/19 20:51
# @Author  : Maple
# @File    : 04-定制描述符的元类.py
# @Software: PyCharm



import abc


class AutoStorage:
    __counter = 0

    def __init__(self):
        cls = self.__class__
        prefix = cls.__name__
        index = cls.__counter
        self.storage_name = '_{}#{}'.format(prefix,index)
        cls.__counter += 1

    def __get__(self, instance, owner):
        if instance is None:
            return self
        else:
            return getattr(instance,self.storage_name)

    def __set__(self, instance, value):
        setattr(instance,self.storage_name,value)


class Validated(abc.ABC,AutoStorage):
    def __set__(self, instance, value):
        value = self.validate(instance,value)
        super().__set__(instance,value)


    @abc.abstractmethod
    def validate(self,instance,value):
        """retuen validated value or raise ValueError"""


class Quantity(Validated):
    """a number greater than zero"""
    def validate(self,instance,value):
        if value > 0:
            return value
        else:
            raise ValueError('value must be >0')

class NonBlank(Validated):
    """a string with at least one non-space character"""
    def validate(self,instance,value):
        value = value.strip()
        if len(value) == 0:
            raise ValueError('value can not be empty or blank')
        else:
            return value


class EntityMeta(type):

    def __init__(cls, name, bases, attr_dict):
        super().__init__(name, bases, attr_dict)
        for key, attr in attr_dict.items():
            if isinstance(attr, Validated):
                type_name = type(attr).__name__
                attr.storage_name = '_{}.{}'.format(type_name, key)



class Entity(metaclass=EntityMeta):
    """带有验证字段的业务实体"""

# LineItem是 Entity 的子类,而子类可以继承父类的元类特性
# 因此LineItem类也会被EntityMeta管控
class LineItem(Entity):

    description = NonBlank()
    weight = Quantity()
    price = Quantity()

    def __init__(self,description,weight,price):
        self.description = description
        self.weight = weight
        self.price = price

    def subtotal(self):
        return self.weight * self.price

    def __repr__(self):

        return 'LineItem =({},{},{})'.format(self.description,self.weight,self.price)


if __name__ == '__main__':

    com = LineItem('Computer', 10, 1000)
    # 查看实例属性的名
    print(com.__dict__)  # {'_NonBlank#description': 'Computer', '_Quantity#weight': 10, '_Quantity#price': 1000}

6、元类的prepare方法

(1)代码示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/19 21:10
# @Author  : Maple
# @File    : 05-元类的prepare方法.py
# @Software: PyCharm



import abc
import collections


class AutoStorage:
    __counter = 0

    def __init__(self):
        cls = self.__class__
        prefix = cls.__name__
        index = cls.__counter
        self.storage_name = '_{}#{}'.format(prefix, index)
        cls.__counter += 1

    def __get__(self, instance, owner):
        if instance is None:
            return self
        else:
            return getattr(instance, self.storage_name)

    def __set__(self, instance, value):
        setattr(instance, self.storage_name, value)


class Validated(abc.ABC, AutoStorage):
    def __set__(self, instance, value):
        value = self.validate(instance, value)
        super().__set__(instance, value)

    @abc.abstractmethod
    def validate(self, instance, value):
        """retuen validated value or raise ValueError"""


class Quantity(Validated):
    """a number greater than zero"""

    def validate(self, instance, value):
        if value > 0:
            return value
        else:
            raise ValueError('value must be >0')


class NonBlank(Validated):
    """a string with at least one non-space character"""

    def validate(self, instance, value):
        value = value.strip()
        if len(value) == 0:
            raise ValueError('value can not be empty or blank')
        else:
            return value


class EntityMeta(type):

    @classmethod
    def __prepare__(metacls, name, bases):
        # 1.该方法先于new和init方法执行
        # 2.该方法必须返回一个映射
        # 3.该方法的返回值会传递给init方法的最后一个参数(本例是attr_dict)
        return collections.OrderedDict()

    def __init__(cls, name, bases, attr_dict):
        # 注意:此时attr_dict是OrderedDict对象,其特点是先进先出
        super().__init__(name, bases, attr_dict)
        # 为类添加一个类属性
        cls._filed_names = []
        for key, attr in attr_dict.items():
            if isinstance(attr, Validated):
                type_name = type(attr).__name__
                attr.storage_name = '_{}.{}'.format(type_name, key)
                cls._filed_names.append(key)


class Entity(metaclass=EntityMeta):
    """带有验证字段的业务实体"""
    @classmethod
    def field_names(cls):
        for key in cls._filed_names:
            yield key


# LineItem是 Entity 的子类,而子类可以继承父类的元类特性
# 因此LineItem类也会被EntityMeta管控
class LineItem(Entity):
    description = NonBlank()
    weight = Quantity()
    price = Quantity()

    def __init__(self, description, weight, price):
        self.description = description
        self.weight = weight
        self.price = price

    def subtotal(self):
        return self.weight * self.price

    def __repr__(self):
        return 'LineItem =({},{},{})'.format(self.description, self.weight, self.price)


if __name__ == '__main__':
    com = LineItem('Computer', 10, 1000)
    # 查看实例属性的名
    print(com.__dict__)  # {'_NonBlank#description': 'Computer', '_Quantity#weight': 10, '_Quantity#price': 1000}

    # 按照添加字段的顺序产出字段的名字
    for name in LineItem.field_names():
        """description
           weight
           price
        """
        print(name)

 (2)LineItem类的实例对象创建流程

 >> step1:调用元类的__prepare__方法,返回一个OrderedDict,该对象会作为参数值传递给元类的__new__方法中的args(说明:是一个元组,包含三个参数,分别是要创建的类的名字;类的父类;类中的方法和属性值)。

>>  step2:调用元类的__new__方法,创建类LineItem(注意此时会扫描类LineItem中定义的属性和方法,然后它们会被传递到new方法中的args参数的第三个位置,如下图所示)

>>  step3:调用元类的__init__方法,初始化类LineItem(注意观察一下,attr_dict参数的值其实就上上一步骤中args元组中第三个位置的值).

在本例的__init__方法中,主要是为类添加了一个_filed_names类属性(是一个列表),然后为其添加LineItem中定义的其他3个类属性(description,weight和price)的值(注意这三个类属性是描述符)。因为OrderedDict中存放的对象(存放流程:扫描类LineItem中定义的属性和方法,依次放到OrderedDict,也因此description,weight和price会被依次放入其中),具有先进先出的特征,所以_filed_names列表中的值的存放顺序也是[description,weight,price]。

>>  step4-8:不再赘述, 详细可参考元类创建类的流程详解[史上最详细]-CSDN博客

三、结语

历史将近4个月,《流畅的Python》完结撒花, 下一站《Python数据科学》,相聚有时,后会有期。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值