views:

78

answers:

2

I have some data that lends itself to representation as a value and a comparison function, (val, f), so another value can be checked against it by seeing if f(val, another) is True. That's easy.

Some of them just need >, <, or == as f, however, and I can't find a clean way of using them; I end up writing things like ScorePoint(60, lambda a, b: a <= b). That's ugly.

Is there a way I can do something more like ScorePoint(60, <=)?

+8  A: 

The operator module is your friend:

import operator
ScorePoint(60, operator.le)

See http://docs.python.org/library/operator.html

RichieHindle
Still ick --- I have to import a module just to treat operators as functions? But that's better than doing it myself. Thanks!
JasonFruit
A: 

Yes:

 LessEqual = lambda a, b: a <= b
 ScorePoint(60, LessEqual)

or more concise (but less readable):

 LE = lambda a, b: a <= b
 ScorePoint(60, LE)
Aaron Digulla
I can't help but notice that you don't have a 'peer pressure' badge yet ;)
aaronasterling
I can't help but notice that people still vote down for no good reason. :-)
Aaron Digulla