# a→ab

## computation, game design, and experimentation

front page | about | archives | code dump | c.s. for mere mortals | tags | rss feed

# Passing a comparison operator in Python

October 28, 2013

So, earlier today I was refactoring some code to be more general purpose, and said code involved a comparison operator. I was already using the same code in two places, but copy-pasted with different comparison operators switched in. Eww.

I didn't quite know how to pull this off, given that I'm not that well versed on Python's introspection faculties... but I gave it a whack. My first thought was the niave approach, of passing in a string to specify mode...

Yuck.

Then I realized that functions are pretty much first class in python. Additionally, lambdas are short and sweet. A few minutes later, I was rolling with this instead:

So much nicer. Instead of passing a mode string, now you just pass one of those comparators, ala:

myfun(4,7,comparator=cmp_lt)


Update: A friend pointed out a few other ways of accomplishing this -- one way more pythonic, and one incredibly unsafe.

The pythonic solution relies on the 'operator' module:

That's much like approach 3, but the call is now:

myfun(4,7,comparator=operator.lt)


The unsafe way is to evaluate it as a string! Here we go:

Man, I love Python. We're not starved for options!

Hope this helps someone else!