about 3 years ago

Annotate Class Attributes with Metaclasses

這個條款講得是應用 metaclasses 來修改 class attributes

使用 descriptor 可以簡單地做到 annotation

class Field(object):
    def __init__(self, name):
        self.name = name
        self.internal_name = '_' + self.name

    def __get__(self, instance, instance_type):
        if instance is None: return self
        return getattr(instance, self.internal_name, '')

    def __set__(self, instance, value):
        setattr(instance, self.internal_name, value)

class Customer(object):
    # Class attributes

    first_name = Field('first_name')
    last_name = Field('last_name')
    prefix = Field('prefix')
    suffix = Field('suffix')

但上面的程式問題在於 class attributes 的名稱跟傳給 Field 的引數重複了,但我們又沒辦法讓 Field 在建立時就知道 attribute 的名稱。

可以用 metaclasses 來解決這個問題,自動對 name 以及 internal_name 賦值。

class Meta(type):
    def __new__(meta, name, bases, class_dict):
        for key, value in class_dict.items():
            if isinstance(value, Field):
                value.name = key
                value.internal_name = '_' + key
        cls = type.__new__(meta, name, bases, class_dict)
        return cls


class DatabaseRow(object, metaclass=Meta):
    pass


class Field(object):
    def __init__(self):
        # These will be assigned by the metaclass.

        self.name = None
        self.internal_name = None

    def __get__(self, instance, instance_type):
        if instance is None: return self
        return getattr(instance, self.internal_name, '')

    def __set__(self, instance, value):
        setattr(instance, self.internal_name, value)


class BetterCustomer(DatabaseRow):
    first_name = Field()
    last_name = Field()
    prefix = Field()
    suffix = Field()
← Effective Python 心得筆記: Item 34 Context in React →
 
comments powered by Disqus