一、核心要义
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: PyCharmdef record_factory(cls_name,field_names):try:field_names = field_names.replace(',',' ').split(' ')except AttributeError:passfield_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)) #Falseprint(Dog1 == Dog2) # False
2、类工厂函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/3/17 20:54
# @Author : Maple
# @File : 02-定制描述符的类装饰器.py
# @Software: PyCharmimport abcdef entity(cls):#keyde的值类似:weight#attr的值类似:<__main__.Quantity object at 0x00000262A8BBA3A0>,是一个描述符对象for key,attr in cls.__dict__.items():if isinstance(attr,Validated):# type_name类似:Quantitytype_name = type(attr).__name__attr.storage_name = '_{}#{}'.format(type_name,key)return clsclass AutoStorage:__counter = 0def __init__(self):cls = self.__class__prefix = cls.__name__index = cls.__counterself.storage_name = '_{}#{}'.format(prefix,index)cls.__counter += 1def __get__(self, instance, owner):if instance is None:return selfelse: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.abstractmethoddef 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 valueelse: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 = descriptionself.weight = weightself.price = pricedef subtotal(self):return self.weight * self.pricedef __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: PyCharmprint('<[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_1return 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_2print('<[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: PyCharmfrom evalsupport import deco_alphaprint('<[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: PyCharmprint('<[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_1return 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_2print('<[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: PyCharmimport abcclass AutoStorage:__counter = 0def __init__(self):cls = self.__class__prefix = cls.__name__index = cls.__counterself.storage_name = '_{}#{}'.format(prefix,index)cls.__counter += 1def __get__(self, instance, owner):if instance is None:return selfelse: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.abstractmethoddef 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 valueelse: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 valueclass 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 = descriptionself.weight = weightself.price = pricedef subtotal(self):return self.weight * self.pricedef __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: PyCharmimport abc
import collectionsclass AutoStorage:__counter = 0def __init__(self):cls = self.__class__prefix = cls.__name__index = cls.__counterself.storage_name = '_{}#{}'.format(prefix, index)cls.__counter += 1def __get__(self, instance, owner):if instance is None:return selfelse: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.abstractmethoddef 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 valueelse: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 valueclass EntityMeta(type):@classmethoddef __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):"""带有验证字段的业务实体"""@classmethoddef 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 = descriptionself.weight = weightself.price = pricedef subtotal(self):return self.weight * self.pricedef __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():"""descriptionweightprice"""print(name)
三、结语
历史将近4个月,《流畅的Python》完结撒花, 下一站《Python数据科学》,相聚有时,后会有期。