As a contrived example, suppose I'm generating a random fruit basket in python. I create the basket:
basket = FruitBasket()
Now I want to specify specific combinations of fruit that can occur in the basket. Suppose I'm a very picky dude, and the basket either has to be full of apples and pomegranates, oranges and grapefruit, or only bananas.
I was reading up on python operator overloading, and it seems like I could define __or__
and __and__
to get the behavior I want. I think I could do something like this:
basket.fruits = (Apple() & Pomegranate()) | (Banana()) | (Orange() & Grapefruit())
This works just fine making two classes (Or
and And
). When __or__
or __and__
get called, I just have return a new Or
or And
object:
def __or__(self, other):return Or(self, other)def __and__(self, other):return And(self, other)
What I'm trying to figure out is how do I do this without having to instantiate the fruits first? Why can't I use a static __or__
method on the base Fruit
class? I've tried this but it doesn't work:
class Fruit(object):@classmethoddef __or__(self, other):return Or(self, other)
and assigning the fruit:
basket.fruits = (Apple & Pomegranate) | (Orange & Grapefruit) | (Banana)
I get an error like this:
TypeError: unsupported operand type(s) for |: 'type' and 'type'
Any thoughts on how to make this work?