1

This must be a FAQ, but I spent five minutes searching, and couldn't find it. In Python, how can I obtain the value that corresponds to a binary operator?

To be more specific; here's an apply-to-8-and-9 function:

def apply_to_8_and_9(fun):
  return fun(8,9)

Now, suppose I want to pass the < function to apply-to-8-and-9. I can do this:

apply_to_8_and_9(lambda a,b: a < b)

... but I'd really rather not have to eta-expand the < operator. Is there some way to avoid this?

Thanks!

John Clements
  • 16,895
  • 3
  • 37
  • 52

1 Answers1

2

Use the operator module. < in particular is operator.lt.

>>> import operator
>>> help(operator.lt)
Help on built-in function lt in module _operator:

lt(...)
    lt(a, b) -- Same as a<b.
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97