Python:在类实例初始化之前修改传递的参数

时间:2012-05-09 18:59:23

标签: python oop multiple-inheritance metaclass

我正在尝试用Python实现简化的术语重写系统(TRS)/符号代数系统的方法。 为此,我真的希望能够在类实例实例化过程中能够拦截和修改特定情况下的操作数。 我提出的解决方案是创建一个元类来修改类对象(类型为'type')的典型调用行为。

class Preprocess(type):
    """
    Operation argument preprocessing Metaclass.
    Classes using this Metaclass must implement the 
        _preprocess_(*operands, **kwargs)
    classmethod.
    """

    def __call__(cls, *operands, **kwargs):
        pops, pargs = cls._preprocess_(*operands, **kwargs)
        return super(Preprocess, cls).__call__(*pops, **pargs)

一个示例案例是扩展嵌套操作F(F(a,b),c) - > F(a,b,c)

class Flat(object):
    """
    Use for associative Operations to expand nested 
    expressions of same Head: F(F(x,y),z) => F(x,y,z)
    """
    __metaclass__ = Preprocess
    @classmethod
    def _preprocess_(cls, *operands, **kwargs):
        head = []
        for o in operands:
            if isinstance(o, cls):
                head += list(o.operands)
            else:
                head.append(o)
        return tuple(head), kwargs

所以,现在可以通过继承来实现这种行为:

class Operation(object):
    def __init__(self, *operands):
        self.operands = operands

class F(Flat, Operation):
    pass

这会导致所需的行为:

print F(F(1,2,3),4,5).operands
(1,2,3,4,5)

但是,我想结合几个这样的预处理类,并让它们按照自然类mro顺序处理操作数。

class Orderless(object):
    """
    Use for commutative Operations to bring into ordered, equivalent 
    form: F(*operands) => F(*sorted(operands))
    """
    __metaclass__ = Preprocess

    @classmethod
    def _preprocess_(cls, *operands, **kwargs):

        return sorted(operands), kwargs

这似乎没有按照要求工作。定义平坦无序操作类型

class G(Flat, Orderless, Expression):
    pass

只导致第一个预处理超类“活跃”。

print G(G(3,2,1),-1,-3).operands
(3,2,1,-1,-3)

如何确保在类实例化之前调用所有预处理类的预处理方法?

更新

由于我作为新的stackoverflow用户的状态,我似乎无法正式回答我的问题。 所以,我相信这可能是我能提出的最佳解决方案:

class Preprocess(type):
    """
    Abstract operation argument preprocessing class.
    Subclasses must implement the 
        _preprocess_(*operands, **kwargs)
    classmethod.
    """

    def __call__(cls, *operands, **kwargs):
        for cc in cls.__mro__:
            if hasattr(cc, "_preprocess_"):
                operands, kwargs = cc._preprocess_(*operands, **kwargs)

        return super(Preprocess, cls).__call__(*operands, **kwargs)

我想问题是super(Preprocess, cls).__call__(*operands, **kwargs)没有像预期的那样遍历cl的mro。

1 个答案:

答案 0 :(得分:1)

我认为你这是错误的做法;删除元类,改为使用类和方法装饰器。

例如,将您的单位定义为:

@init_args_preprocessor
def flat(operands, kwargs): # No need for asterisks
    head = []
    for o in operands:
        if isinstance(o, cls):
            head += list(o.operands)
        else:
            head.append(o)
    return tuple(head), kwargs

使用init_args_preprocessor装饰器将该函数转换为类装饰器:

def init_args_preprocessor(preprocessor):
    def class_decorator(cls):
        orig_init = cls.__init__
        def new_init(self, *args, **kwargs):
            args, kwargs = preprocessor(args, kwargs)
            orig_init(self, *args, **kwargs)
        cls.__init__ = new_init
        return cls
   return class_decorator

现在,而不是mixins使用装饰器:

class Operation(object):
    def __init__(self, *operands):
        self.operands = operands

@flat
class F(Operation):
    pass

你应该没有问题干净地组合类修饰符:

@init_args_preprocessor
def orderless(args, kwargs):
    return sorted(args), kwargs

@orderless
@flat
class G(Expression):
   pass

警告Emptor:以上所有严格未经测试的代码。