相关疑难解决方法(0)

5409
推荐指数
22
解决办法
75万
查看次数

元类和 __prepare__ ()

我正在自学这个__prepare__功能。我在PEP3115看到了这个片段

# The custom dictionary
class member_table(dict):
    def __init__(self):
        self.member_names = []

    def __setitem__(self, key, value):
        # if the key is not already defined, add to the
        # list of keys.
        if key not in self:
            self.member_names.append(key)

        # Call superclass
        dict.__setitem__(self, key, value)

# The metaclass
class OrderedClass(type):

    # The prepare function
    @classmethod
    def __prepare__(metacls, name, bases): # No keywords in this case
        return member_table()

    # The metaclass invocation
    def __new__(cls, name, bases, classdict):
        # Note …
Run Code Online (Sandbox Code Playgroud)

python metaclass

11
推荐指数
1
解决办法
3518
查看次数

标签 统计

metaclass ×2

python ×2

oop ×1

python-datamodel ×1