Skip to content

Instantly share code, notes, and snippets.

@ctbarna
Created August 7, 2012 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctbarna/3289697 to your computer and use it in GitHub Desktop.
Save ctbarna/3289697 to your computer and use it in GitHub Desktop.
Abstracting comparisons in Python
import operator
def do_this (operation, a, b):
return getattr(operator, operation)(a, b)
do_this('eq', 1, 1) # returns True
do_this('gt', 1, 0) # returns True
do_this('le', 1, 0) # returns False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment