锦通 发表于 2024-12-12 22:37:43

Python 元类(Meta Class):解密 Python 面向对象编程的幕后推手

在 Python 编程中,我们每天都在和类打交道,但是你是否也和我一样想过:类本身是什么?是谁创建了类?元类(Meta Class)就是用来创建类的"类"。本日让我们一起深入明白这个强盛而秘密的特性。
从一个简单的类提及

class Person:
    def __init__(self, name):
      self.name = name
   
    def greet(self):
      return f"Hello, I'm {self.name}"

# 创建实例
p = Person("Alice")
print(p.greet())# 输出: Hello, I'm Alice当我们定义这个类时,Python 实际上在背后做了什么?让我们用 type 来看看:
print(type(p))      # <class '__main__.Person'>
print(type(Person))   # <class 'type'>看到了吗?Person 类的范例是 type。实际上,type 就是 Python 中的默认元类。
用 type 动态创建类

在 Python 中,我们可以用 type 动态创建类:
def greet(self):
    return f"Hello, I'm {self.name}"

# 动态创建类
PersonType = type('PersonType',
               (object,),                # 基类
               {
                     '__init__': lambda self, name: setattr(self, 'name', name),
                     'greet': greet
               })

# 使用动态创建的类
p = PersonType("Bob")
print(p.greet())# 输出: Hello, I'm Bob是的,我也很奇怪。 Python 中的 type 函数有两个用法,二者意义相去甚远:

[*]type(name, bases, dict):创建一个新的类对象
[*]type(object):返回对象的范例
自定义元类

当我们需要在类创建时进行一些特殊的控制或修改时,就可以利用自定义元类:
class LoggedMeta(type):
    def __new__(cls, name, bases, attrs):
      # 在类创建前,为所有方法添加日志
      for key, value in attrs.items():
            if callable(value) and not key.startswith('__'):
                attrs = cls.log_call(value)
      
      return super().__new__(cls, name, bases, attrs)
   
    @staticmethod
    def log_call(func):
      def wrapper(*args, **kwargs):
            print(f"Calling method: {func.__name__}")
            return func(*args, **kwargs)
      return wrapper

# 使用自定义元类
class MyClass(metaclass=LoggedMeta):
    def foo(self):
      print("foo")

    def bar(self):
      print("bar")

# 测试
obj = MyClass()
obj.foo()# 输出: Calling method: foo \n foo
obj.bar()# 输出: Calling method: bar \n bar输出:
Calling method: foo
foo
Calling method: bar
bar与继承的区别?

[*]继承是在实例创建时起作用,而元类是在类定义时就起作用
[*]继承控制的是实例的行为,而元类控制的是类的行为
[*]继承遵循 MRO (Method Resolution Order) 规则,而元类工作在更底层,在类被创建之前就介入
继承实现上述的功能:
class Logged:
    def __getattribute__(self, name):
      attr = super().__getattribute__(name)
      if callable(attr) and not name.startswith('__'):
            print(f"Calling method: {name}")
      return attr

class MyClass(Logged):
    def foo(self):
      print("foo")

    def bar(self):
      print("bar")

# 测试
obj = MyClass()
obj.foo()
obj.bar()这种继承方案和元类方案的关键区别是:

[*]继承方案在每次调用方法时都要经过 __getattribute__ ,性能开销较大
[*]元类方案在类定义时就完成了方法的包装,运行时几乎没有额外开销
[*]继承方案更容易明白和调试,元类方案更底层和强盛
这里补充一下 __getattribute__ ,参考: A key difference between __getattr__ and __getattribute__ is that __getattr__ is only invoked if the attribute wasn't found the usual ways. It's good for implementing a fallback for missing attributes, and is probably the one of two you want. 翻译: __getattr__ 和 __getattribute__ 之间的一个关键区别是,只有当属性无法通过通例方式找到时,才会调用 __getattr__ 。它非常恰当实现缺失属性的后备,并且可能是您想要的两个方法之一。
元类的实际应用场景

1. 接口逼迫实现

from abc import ABCMeta, abstractmethod

class InterfaceMeta(ABCMeta):
    def __init_subclass__(cls, **kwargs):
      super().__init_subclass__(**kwargs)
      # 获取所有抽象方法
      abstracts = {name for name, value in cls.__dict__.items()
                     if getattr(value, "__isabstractmethod__", False)}
      # 检查子类是否实现了所有抽象方法
      if abstracts and not getattr(cls, '__abstractmethods__', False):
            raise TypeError(f"Can't instantiate abstract class {cls.__name__} "
                            f"with abstract methods {abstracts}")


class Interface(metaclass=InterfaceMeta):
    @abstractmethod
    def my_interface(self):
      pass


# 这个类会在实例化时报错
class BadImplementation(Interface):
    pass


# 这个类可以正常使用
class GoodImplementation(Interface):
    def my_interface(self):
      return "Implementation"


# 测试
try:
    good = GoodImplementation()# 正常
    print("GoodImplementation instantiated successfully:", good.my_interface())
except TypeError as e:
    print("Error in GoodImplementation:", e)

try:
    bad = BadImplementation()# TypeError: Can't instantiate abstract class...
except TypeError as e:
    print("Error in BadImplementation:", e)留意这里的 __init_subclass__ 方法,它在子类被定义时被调用。在这个方法中,我们查抄子类是否实现了全部抽象方法。如果没有实现,我们就抛出一个 TypeError 异常。
或许出于 Python 动态范例的特性,我们依然只能在 bad = BadImplementation() 实例化时才会报错,而不是像静态语言那样,在 class BadImplementation 定义时就报错。
借助 pylint 这类静态代码查抄工具,我们可以在 class BadImplementation 定义时就发现这个错误。但是 Python 语言本身似乎做不到(或许你有好办法?可以批评区告诉我)。
但这也要比 class Interface 中定义一个 raise NotImplementedError 更优雅一些?
2. ORM 框架中的应用

这是一个简化版的 ORM 示例,展示了元类在实际项目中的应用:
class ModelMeta(type):
    def __new__(cls, name, bases, attrs):
      fields = {}
      for key, value in attrs.items():
            if isinstance(value, Field):
                fields = value
               
      attrs['_fields'] = fields
      return super().__new__(cls, name, bases, attrs)

class Field:
    def __init__(self, field_type):
      self.field_type = field_type

class Model(metaclass=ModelMeta):
    def __init__(self, **kwargs):
      for key, value in kwargs.items():
            if key in self._fields:
                setattr(self, key, value)
   
    def validate(self):
      for name, field in self._fields.items():
            value = getattr(self, name, None)
            if not isinstance(value, field.field_type):
                raise TypeError(f"{name} must be of type {field.field_type}")

# 使用这个简单的 ORM
class User(Model):
    name = Field(str)
    age = Field(int)

# 测试
user = User(name="Alice", age=25)
user.validate()# 正常
user.age = "not an integer"
try:
    user.validate()# 将抛出 TypeError
except TypeError as e:
    print(e)利用元类的留意事项


[*]不要过度利用:元类是强盛的工具,但也容易导致代码难以明白和维护。大多数情况下,普通的类和装饰器就足够了。
[*]性能思量:元类会在类创建时执行额外的代码,如果利用不当可能影响性能。
[*]调试困难:利用元类的代码每每较难调试,因为它们改变了类的创建过程。
总结

元类是 Python 中一个强盛的特性,它允许我们控制类的创建过程。虽然在日常编程中可能用不到,但在框架开发中经常会用到。明白元类的工作原理对于深入明白 Python 的范例系统很有帮助。
末了提示一下,请记住 Python 之禅中的一句话:
Simple is better than complex.
除非确实需要元类的强盛功能,否则利用更简单的解决方案可能是更好的选择。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Python 元类(Meta Class):解密 Python 面向对象编程的幕后推手