将元类规则应用于所有后代而不是直接metaclassed类

时间:2012-05-23 19:37:10

标签: python abstract-class metaclass class-members

首先让我来定义我的目标:

  1. 启用没有默认值的抽象类成员(不是属性,方法或实例成员)的定义(不是None或其他一些魔术值,但如果没有实现则抛出错误)。

  2. 在(1)的服务中,创建一个可重用的抽象机制,使得创建具有抽象成员的类变得微不足道,并且这样做的代码最简洁。

  3. 能够将抽象类成员附加到父类(作为子类,元类或任何其他方法),在定义具体子类之前,不需要定义抽象成员。

  4. 到目前为止我得到了什么:

    metaclass_lib.py:

    class AbstractRequiredClassMembersMetaclass(type):
    
        def __new__(cls, name, bases, attrs):
    
            missed_members = []
            for class_member in cls.REQUIRED_CLASS_MEMBERS:
                if class_member not in attrs:
                    missed_members.append(class_member)
            if missed_members:
                raise NotImplementedError('Class missing required members %s.' % missed_members)
            return super(AbstractRequiredClassMembersMetaclass, cls).__new__(cls, name, bases, attrs)
    
    
    
    class _MakeRequiredClassMemebersMetaclass(object):
    
        existing_classes = {}
    
        def __call__(self, name, required_members):
    
            if name in _MakeRequiredClassMemebersMetaclass.exisiting_classes:
                if required_members != _MakeRequiredClassMemebersMetaclass.exisiting_classes[name].REQUIRED_CLASS_MEMBERS:
                    raise RuntimeError('Class of name %s already defined with different required_members.' % name)
            else:
                NewClass = type(name, (AbstractRequiredClassMembersMetaclass,), {'REQUIRED_CLASS_MEMBERS' : required_members})
                _MakeRequiredClassMemebersMetaclass.exisiting_classes[name] = NewClass
            return _MakeRequiredClassMemebersMetaclass.exisiting_classes[name]
    
    make_required_class_members_metaclass = _MakeRequiredClassMemebersMetaclass()
    

    goods.py(用于说明的实施):

    from metaclass_lib import make_required_class_members_metaclass
    
    class AbstractGood(object):
    
        __metaclass__ = make_required_class_members_metaclass('AbstractGoodMeta', ('unit_measure',))
    
        def __init__(self):
    
            self.unit_price = None
    
        def total_cost(self, number_of_units):
    
            return self.unit_price * self.number_of_units
    
    
    
    class DeliGood(AbstractGood):
    
        unit_measure = 'lbs'
    
        def __init__(self):
    
            self.sandwich_counter_product = False
    
    
    
    class RefridgeratedGood(AbstractGood):
    
        unit_measure = 'package'
    
        def __init__(self):
    
            self.freezer_safe = False
            self.optimal_temp = '35'
    

    这不起作用,因为在创建AbstractGood type对象期间,元类会窒息。问题是我想要所有具体商品来定义类成员,但我不想在任何抽象基础中定义类成员。我能想到的只是让元类只进行in attrs检查关键字是否在name(ex if 'Abstract' not in name)中,但这看起来很脆弱而且很脆弱。

    有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

也是一个黑客,但比检查名称更可靠。 您知道抽象基类没有基类 (或者在python 2中只有object类型)。所以你可以检查一下:)

class AbstractBase(object):
   class __metaclass__(type):
        def __new__(mcs, name, bases, dict_):
           if bases[0] is not object: # hack to not apply these on AbstractBase
                assert 'unit_measure' in dict_
           return type.__new__(mcs, name, bases, dict_)


class DeliGood(AbstractBase):
    unit_measure = 'lbs'