在python类上重写__or__运算符

Dar*_*ckt 5 python operator-overloading

作为一个人为的例子,假设我在python中生成一个随机的水果篮。我创建了购物篮:

basket = FruitBasket()
Run Code Online (Sandbox Code Playgroud)

现在,我想指定篮子中可能出现的水果的特定组合。假设我是一个非常挑剔的家伙,篮子必须装满苹果和石榴,桔子和葡萄柚,或者只装香蕉。

我正在阅读python运算符重载,似乎可以定义__or____and__获取所需的行为。我想我可以做这样的事情:

basket.fruits = (Apple() & Pomegranate()) | (Banana()) | (Orange() & Grapefruit())
Run Code Online (Sandbox Code Playgroud)

制作两个类(OrAnd)效果很好。当__or__或被__and__调用时,我只返回了一个新对象OrAnd对象:

def __or__(self, other):
    return Or(self, other)

def __and__(self, other):
    return And(self, other)
Run Code Online (Sandbox Code Playgroud)

我要弄清楚的是如何做到这一点而不必先实例化水果?为什么不能__or__在基Fruit类上使用静态方法?我已经尝试过了,但是没有用:

class Fruit(object):
    @classmethod
    def __or__(self, other):
        return Or(self, other)
Run Code Online (Sandbox Code Playgroud)

并分配水果:

basket.fruits = (Apple & Pomegranate) | (Orange & Grapefruit) | (Banana)
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误:

TypeError: unsupported operand type(s) for |: 'type' and 'type'
Run Code Online (Sandbox Code Playgroud)

对如何进行这项工作有任何想法吗?

Chr*_*gan 5

__or__查找对象的类型;一个Fruit例子,那将是Fruit; 因为Fruit,那是typeFruit不过,您可以使用元类来更改的类型:

class FruitMeta(type):

    def __or__(self, other):
        return Or(self, other)


class Fruit(object):
    __metaclass__ = FruitMeta
Run Code Online (Sandbox Code Playgroud)

(对于Python 3,语法class Fruit(metaclass=FruitMeta):改为。)

然后,这完成了您想要的所有操作。Apple | Banana(假设这两个是的子类Fruit)将产生Or(Apple, Banana)

但是,请务必谨慎使用这种设计。它趋于进入魔术领域,并可能容易引起混乱。

(完整的演示,在Python 2.7中:)

>>> class Or(object):
...     def __init__(self, a, b):
...             self.a = a
...             self.b = b
...     def __repr__(self):
...             return 'Or({!r}, {!r})'.format(self.a, self.b)
... 
>>> class FruitMeta(type):
...     def __or__(self, other):
...             return Or(self, other)
... 
>>> class Fruit(object):
...     __metaclass__ = FruitMeta
... 
>>> class Apple(Fruit): pass
... 
>>> class Banana(Fruit): pass
... 
>>> Apple | Banana
Or(<class '__main__.Apple'>, <class '__main__.Banana'>)
Run Code Online (Sandbox Code Playgroud)