流畅的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方法

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

三、结语

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/274386.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

框架篇常见面试题

1、Spring框架的单例bean是线程安全的吗&#xff1f; 2、什么是AOP&#xff1f; 3、Spring的事务是如何实现的&#xff1f; 4、Spring事务失效的场景 5、SpringBean的声明周期 6、Spring的循环依赖 7、SpringMVC的执行流程 8、SpringBoot自动配置原理 9、Spring常见注解

mysql重构

力扣题目链接 列转行 SELECT product_id, store1 store, store1 price FROM products WHERE store1 IS NOT NULL UNION SELECT product_id, store2 store, store2 price FROM products WHERE store2 IS NOT NULL UNION SELECT product_id, store3 store, store3 price FROM p…

Livox激光雷达 mid360 跑 fastlio2 - 流程记录

mid360 跑 fastlio2 一、配置 mid360 环境1.1、主机配置静态IP为192.168.1.501.2、Livox-SDK21.3、Livox_ros_driver2二、Fast-lio22.1、下载源码2.2、修改代码2.3、编译、运行 提示一下&#xff0c;如果在一些板上&#xff08;比如rk3399&#xff0c;或者是树莓派 &#xff0…

晶圆制造过程中常用载具的类型

晶圆载具用于硅片生产、晶圆制造以及工厂之间晶圆的储存、传送、运输以及防护。晶圆载具种类很多,如FOUP用于晶圆制造工厂中晶圆的传送;FOSB用于硅片生产与晶圆制造工厂之间的运输;CASSETTE载具可用于工序间运送以及配合工艺使用。 OPEN CASSETTE OPEN CASSETTE主要在晶圆…

QT自定义带参数信号与槽函数

我的软件界面是这样的&#xff0c;点击图标按钮后进入子项的参数配置。 由于按钮众多&#xff0c;每个按钮都有点击事件&#xff0c;一个个去写是在太多麻烦&#xff0c;而且我的这个配置软件各个子项的参数配置页面基本是差不多的&#xff0c;只是传递的参数有差异而已。 通过…

PHP+MySQL开发组合:多端多商户DIY商城源码系统 带完整的搭建教程以及安装代码包

近年来&#xff0c;电商行业的迅猛发展&#xff0c;越来越多的商户开始寻求搭建自己的在线商城。然而&#xff0c;传统的商城系统往往功能单一&#xff0c;无法满足商户个性化、多样化的需求。同时&#xff0c;搭建一个功能完善的商城系统需要专业的技术团队和大量的时间成本&a…

【FPGA】摄像头模块OV5640

本篇文章包含的内容 一、OV5640简介1.1 基本概述1.2 工作时序1.2.1 DVP Timing&#xff08;数据传输时序&#xff09;1.2.2 帧曝光工作模式 1.3 OV5640 闪光灯工作模式1.3.1 Xenon Flash&#xff08;氙灯闪烁&#xff09;模式1.3.2 LED 1&2 模式1.3.3 LED 3模式1.3.4 手动开…

【ESP32接入国产大模型之MiniMax】

1. MiniMax 讲解视频&#xff1a; ESP32接入语言大模型之MiniMax MM智能助理是一款由MiniMax自研的&#xff0c;没有调用其他产品的接口的大型语言模型。MiniMax是一家中国科技公司&#xff0c;一直致力于进行大模型相关的研究。 随着人工智能技术的不断发展&#xff0c;自然语…

Python入门(小白友好)

知识图谱 搭建环境 安装Python:Download Python | Python.org 安装PyCharm:Download PyCharm: The Python IDE for data science and web development by JetBrains 注意:专业版本是收费的,新手小白使用社区版(community)即可 创建第一个项目: 一些PyCharm的设置(也适用…

Springboot和Spring Cloud版本对应

Spring在不断地升级&#xff0c;各个版本存在一些不兼容的地方&#xff0c;为了避免出现问题&#xff0c;最好注意使用正确的版本。 官网的对应关系&#xff1a;https://start.spring.io/actuator/info 如下图&#xff1a; 下面附一下创建项目的工具&#xff1a; Spring官方…

ClickHouse--13--springboot+mybatis配置clickhouse

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 ClickHouse1.添加maven依赖2.配属数据源3.参数配置4.Druid连接池配置5.entity6.Mapper接口7.Mapper.xml8.controller接口9.创建一个clickhouse表10.测试 ClickHouse…

【复现】【免费】基于多时间尺度滚动优化的多能源微网双层调度模型

目录 主要内容 部分代码 结果一览 1.原文结果 2.程序运行结果 下载链接 主要内容 该模型参考《Collaborative Autonomous Optimization of Interconnected Multi-Energy Systems with Two-Stage Transactive Control Framework》&#xff0c;主要解决的是一个多…

springboot校服订购系统

摘 要 本文首先实现了校服订购系统设计与实现管理技术的发展随后依照传统的软件开发流程&#xff0c;最先为系统挑选适用的言语和软件开发平台&#xff0c;依据需求分析开展控制模块制做和数据库查询构造设计&#xff0c;随后依据系统整体功能模块的设计&#xff0c;制作系统的…

阿里云发布 AI 编程助手 “通义灵码”——VSCode更强了 !!

文章目录 什么是 通义灵码&#xff08;TONGYI Lingma&#xff09; 快速体验“通义灵码” 什么是“通义灵码”&#xff08;TONGYI Lingma&#xff09; 通义灵码&#xff08;TONGYI Lingma&#xff09;&#xff0c;是阿里云出品的一款基于通义大模型的智能编码辅助工具&#xff…

考研失败, 学点Java打小工_Day3_卫语句_循环

1 编码规范——卫语句 表达异常分支时&#xff0c;少用if-else方式。   比如成绩判断中对于非法输入的处理&#xff1a; /*>90 <100 优秀>80 <90 良好>70 <80 一般>60 <70 及格<60 不及格*/Testpu…

阿里云2核4G4M轻量应用服务器价格165元一年

阿里云优惠活动&#xff0c;2核4G4M轻量应用服务器价格165元一年&#xff0c;4Mbps带宽下载速度峰值可达512KB/秒&#xff0c;系统盘是60GB高效云盘&#xff0c;不限制月流量&#xff0c;2核2G3M带宽轻量服务器一年87元12个月&#xff0c;在阿里云CLUB中心查看 aliyun.club 当前…

[QJS xmake] 非常简单地在Windows下编译QuickJS!

文章目录 前言准备C编译器xmake编译包 工程准备修改版本号第一遍编译第二遍编译效果 前言 quickjs是个很厉害的东西啊&#xff0c;我一直想编译一下的&#xff0c;奈何一直没成功。现在找了点时间成功编译了&#xff0c;写篇文章记录一下。当前版本&#xff1a;2024-1-13 应该…

MySQL数据自动同步到Es

Logstash 测试数据准备 DROP DATABASE IF EXISTS es;CREATE DATABASE es DEFAULT CHARACTER SET utf8;USE es;CREATE TABLE book (id INT NOT NULL,title VARCHAR(20),author VARCHAR(20),price DECIMAL(6,2),PRIMARY KEY(id) );DROP PROCEDURE IF EXISTS batchInsertBook;DELI…

关系数据库:关系数据结构基础与概念解析

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

代码随想录算法训练营第二十八天|93. 复原 IP 地址,78. 子集,90. 子集 II

93. 复原 IP 地址 题目 有效 IP 地址 正好由四个整数&#xff08;每个整数位于 0 到 255 之间组成&#xff0c;且不能含有前导 0&#xff09;&#xff0c;整数之间用 ‘.’ 分隔。 例如&#xff1a;“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址&#xff0c;但是 “0.0…
最新文章