关于python:python-中是先运行-metaclass-还是先有类属性

39次阅读

共计 1260 个字符,预计需要花费 4 分钟才能阅读完成。

答案:先有“类属性”,再有“运行 metaclass”

# 定义一个元类
class CustomMetaclass(type):
    def __new__(cls, name, bases, attrs):
        print('> cls', cls)
        print('> name', name)
        print('> attrs', attrs)
        print('> cls dict', cls.__dict__)
        # 在创立类时批改属性
        new_attrs = {}
        for attr_name, attr_value in attrs.items():
            if isinstance(attr_value, str):
                new_attrs[attr_name] = attr_value.upper()
            else:
                new_attrs[attr_name] = attr_value

        obj = super().__new__(cls, name, bases, new_attrs)

        print(obj.__dict__)
        
        print(type(obj))
        
        return obj

# 应用元类创立类


class MyClass(metaclass=CustomMetaclass):
    name = 'John'
    age = 30
    greeting = 'Hello'

    def say_hello(self):
        print(self.greeting)


# 创立类的实例并调用办法
obj = MyClass()
print(obj.name)         # 输入: 'JOHN'
print(obj.age)          # 输入: 30
obj.say_hello()         # 输入: 'Hello'

输入后果如下:

> cls <class '__main__.CustomMetaclass'>
> name MyClass
> attrs {'__module__': '__main__', '__qualname__': 'MyClass', 'name': 'John', 'age': 30, 'greeting': 'Hello', 'say_hello': <function MyClass.say_hello at 0x1025c2200>}
> cls dict {'__module__': '__main__', '__new__': <staticmethod(<function CustomMetaclass.__new__ at 0x1025c2290>)>, '__doc__': None}
{'__module__': '__MAIN__', 'name': 'JOHN', 'age': 30, 'greeting': 'HELLO', 'say_hello': <function MyClass.say_hello at 0x1025c2200>, '__dict__': <attribute '__dict__' of 'MyClass' objects>, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None}
<class '__main__.CustomMetaclass'>
JOHN
30

正文完
 0