python元类没看懂

问题描述 投票:0回答:0
class MyMeta(type):
    def __init__(self, name, bases, attrs):
        pass
        
    def __new__(cls, name, bases, attrs):
        attrs['desc'] = lambda self: "the test"
        return super().__new__(cls, name, bases, attrs)
class MyMeta(type):
    def __init__(self, name, bases, attrs):
        self.desc = lambda self: "the test"

    def __new__(cls, name, bases, attrs):
        return super().__new__(cls, name, bases, attrs)
b = Bar()
print(b.desc())

将desc添加到new和将其添加到init有什么区别?结果是一样的

将desc添加到new和将其添加到init有什么区别?结果是一样的

python-3.x metaclass
© www.soinside.com 2019 - 2024. All rights reserved.