title: Python 3 infix operators
body: <p>I was looking into python infix operators and I found this: <a href="https://stackoverflow.com/questions/932328/python-defining-my-own-operators">Python: defining my own operators?</a></p>
  
  <p>It was asked a very long time ago but I can't find anything more recent. It seems like more of a hack than I'd normally be comfortable doing. Using that hack, I've transformed my code:</p>
  
  <pre class="lang-py prettyprint-override"><code>if __name__ == '__main__':
      of = Infix(lambda x, y: Card(Value[x], Suit[y]))
  
      card1 = 'EIGHT' | of | 'DIAMONDS'
      card2 = 'THREE' | of | 'SPADES'
      card3 = 'QUEEN' | of | 'SPADES'
  
      assert card1.suit != card2.suit
      assert card3.suit == card2.suit
  
      assert ('NINE' | of | 'HEARTS').beats('EIGHT' | of | 'HEARTS')
      assert ('NINE' | of | 'HEARTS').beats('EIGHT' | of | 'HEARTS', trump=Suit.HEARTS)
      assert ('NINE' | of | 'HEARTS').beats('EIGHT' | of | 'HEARTS', trump=Suit.DIAMONDS)
  
      assert not Card(Value.NINE, Suit.HEARTS).beats(Card(Value.TEN, Suit.HEARTS))
      assert not Card(Value.NINE, Suit.HEARTS).beats(Card(Value.TEN, Suit.HEARTS), trump=Suit.HEARTS)
      assert not Card(Value.NINE, Suit.HEARTS).beats(Card(Value.TEN, Suit.HEARTS), trump=Suit.DIAMONDS)
  
      assert not Card(Value.NINE, Suit.DIAMONDS).beats(Card(Value.EIGHT, Suit.HEARTS))
      assert not Card(Value.NINE, Suit.DIAMONDS).beats(Card(Value.EIGHT, Suit.HEARTS), Suit.HEARTS)
      assert not Card(Value.NINE, Suit.DIAMONDS).beats(Card(Value.EIGHT, Suit.HEARTS), Suit.CLUBS)
  </code></pre>
  
  <p>You can see what the code looks like with and without, and I think it's WAY more readable like this. Still, it's a hack, and I also am not a fan of accessing the enums by string. I am still considering:</p>
  
  <pre class="lang-py prettyprint-override"><code>EIGHT = Value.EIGHT
  SPADES = Suit.SPADES
  
  card = Card(EIGHT, SPADES)
  </code></pre>
  
  <p>I could even combine the two approaches and change the Infix to:</p>
  
  <pre class="lang-py prettyprint-override"><code>   of = Infix(lambda x,y: Card(x, y))
  
     card = EIGHT | of | SPADES
  
  </code></pre>
  
  <p>Is there a better way of doing the infix thing in Python 3? Is it worth doing at all?</p>
  
  <p>EDIT:</p>
  
  <pre class="lang-py prettyprint-override"><code>    assert not (NINE | of | HEARTS).beats(TEN | of | HEARTS)
      assert not (NINE | of | HEARTS).beats(TEN | of | HEARTS, trump=HEARTS)
      assert not (NINE | of | HEARTS).beats(TEN | of | HEARTS, trump=DIAMONDS)
  
      assert not Card(NINE, DIAMONDS).beats(Card(EIGHT, HEARTS))
      assert not Card(NINE, DIAMONDS).beats(Card(EIGHT, HEARTS), trump=HEARTS)
      assert not Card(NINE, DIAMONDS).beats(Card(EIGHT, HEARTS), trump=CLUBS)
  </code></pre>
  
  <p>One comment may be suggesting that the first block is no better than the second. I can see the logic there. This includes the whole <code>EIGHT = Value.EIGHT</code> thing which I think is good. (Can you tell I come from compiled-languages-land?)</p>