How to use infix operators as higher order functions? - python

Is there any way to use infix operators (like +,-,*,/) as higher order functions in python without creating "wrapper" functions?
def apply(f,a,b):
return f(a,b)
def plus(a,b):
return a + b
# This will work fine
apply(plus,1,1)
# Is there any way to get this working?
apply(+,1,1)

You can use the operator module, which has the "wrapper" functions written for you already.
import operator
def apply(f,a,b):
return f(a,b)
print apply(operator.add,1,1)
Result:
2
You can also define the wrapper using lambda functions, which saves you the trouble of a standalone def:
print apply(lamba a,b: a+b, 1, 1)

Use operator module and a dictionary:
>>> from operator import add, mul, sub, div, mod
>>> dic = {'+':add, '*':mul, '/':div, '%': mod, '-':sub}
>>> def apply(op, x, y):
return dic[op](x,y)
...
>>> apply('+',1,5)
6
>>> apply('-',1,5)
-4
>>> apply('%',1,5)
1
>>> apply('*',1,5)
5
Note that you can't use +, -, etc directly as they are not valid identifiers in python.

You can use the operator module this way:
import operator
def apply(op, a, b):
return op(a, b)
print(apply(operator.add, 1, 2))
print(apply(operator.lt, 1, 2))
Output:
3
True
The other solution is to use a lambda function, but "there should be one -- and preferably only one -- obvious way to do it", so I prefer to use the operator module

you can use anonymous function : apply(lambda x,y : x + y, 1,1)

# Is there any way to get this working?
apply(+,1,1)
No. As others have already mentioned, there are function forms of all of the operators in the operator module. But, you can't use the operators themselves as that is a SyntaxError and there is no way to dynamically change python's core syntax. You can get close though using dictionaries and passing strings:
_mapping = {'+':operator.add}
def apply(op,*args):
return _mapping[op](*args)
apply('+',1,1)

It is possible to give the operators +, -, *, and / special behavior for a class using magic methods, you can read about this here: http://www.rafekettler.com/magicmethods.html
This isn't exactly what you were asking for because this still requires the creation of a method for each operator, but it does allow you to use the operators by symbol in your code. Note that I don't think this is better than the other methods, it is just an illustration of how you can define behavior for operators:
class Prefix(object):
def __add__(self, other):
""" Prefix() + (a, b) == a + b """
return other[0] + other[1]
def __sub__(self, other):
""" Prefix() - (a, b) == a - b """
return other[0] - other[1]
def __mul__(self, other):
""" Prefix() * (a, b) == a * b """
return other[0] * other[1]
def __div__(self, other):
""" Prefix() / (a, b) == a / b """
return other[0] / other[1]
And examples:
>>> prefix = Prefix()
>>> prefix + (12, 3)
15
>>> prefix - (12, 3)
9
>>> prefix * (12, 3)
36
>>> prefix / (12, 3)
4
Of course this method can't be used for a more complex prefix equation like * / 6 2 5 because there is no way to define behavior for adjacent operators, which will always give a SyntaxError (except for a few special cases where + or - are interpreted as making the next element positive or negative).

Related

Python: Comparison in function argument

I am currently working on the following problem:
I want to write a function, that accepts comparisons as arguments - but want to prevent, that these comparisons are evaluated during runtime (leading to potentially unexpected results).
To be more precise, I have the following challenge (first, original one in pySpark, then similar, more general one):
def test_func(*comparisons):
for comparison in comparison:
left_hand = comparison[0]
comparison = comparison[1]
right_hand = comparison[2]
Example 1:
test_func(F.col('a') == F.col('b'))
left_hand -> F.col('a')
right_hand -> F.col('b')
comparison -> ==
Example 2:
test_func(1 <=> 2)
left_hand -> 1
right_hand -> 2
comparison -> <=>
Right now, the equation/parameter is evaluated before it reaches the function - i.e., I have problems splitting the equation into it individual parts.
Is this even possible to like this?
The python operator module stores operators as functions
from operator import *
def test_func(*comparison):
left_hand = comparison[0]
comparison = comparison[1]
right_hand = comparison[2]
test_func(F.col('a'), eq, F.col('b'))
The variables would be (remember they would still be local to test_func):
left_hand = F.col('a')
comparison = eq -> operator.eq
right_hand = F.col('b')
As a quick proof of concept:
>>> import operator
>>> class Col:
... def __init__(self, col):
... self.col = col
...
... def __eq__(self, other):
... return self, operator.eq, other
...
>>> Col('a') == Col('b')
(<__main__.Col object at 0x11134d5b0>, <built-in function eq>, <__main__.Col object at 0x11147cbe0>)
>>> lh, comp, rh = Col('a') == Col('b')
>>> comp(lh.col, rh.col)
False
You'll need to overload all special methods for all operators you want to support, and return the equivalent operator function (or whatever you want, perhaps '==', or a custom object).

Is it possible to define a new mathematical operator in python?

Is it possible to define my own mathematical operator?
Like the introduction of the new # in PEP-465 for matrix multiplication or // for integer division.
Something like
a ! b -> a / (a + b)
You can use one of the predefined operator that has not been implemented for integers.
For example this one : # (matmul)
You cannot use ! character as it is not in this list.
class MyInt(int):
def __matmul__(self, other):
return self / (self + other)
Then you can do :
a = MyInt(1)
b = MyInt(2)
print(a # b)
Will print (0.33333333)

Python - use an operator at runtime

Is it possible to insert an operation (e.g *, +) between two variables at runtime?
My solution without doing this is multiple if, elif statements, but I don't think that's the most efficient way to do it.
EDIT: What I meant is I get two integers, and I want to apply an operation on one of them with the other, e.g x * y, but I want to change * to another operator (maybe they're called functions? Not sure) e.g -, +,^ based on input.
Does that make sense? Basically think of it as a calculator.
I'm not sure if this is what you're looking for but the operator module has a lot of operations, e.g. add and mul (multiply):
import operator
var_1 = 2
var_2 = 3
print(operator.add(var_1, var_2))
print(operator.mul(var_1, var_2))
will print
5
6
#AaronHall's is the answer you're looking for, but for completeness, I'd mention you can also use eval.
var_1 = 2
var_2 = 3
op = '+'
print eval('%s%s%s' % (var_1, op, var_2))
However, eval is evil, so either don't use it, or use with caution.
To answer the follow-on question, for example, you can subclass int and then implement __xor__ to exponentiate instead of apply a bitwise or, which is called by the ^ operator:
import operator
class MyInt(int):
def __xor__(self, other):
return operator.pow(self, other)
and then:
>>> i = MyInt(2)
>>> i
2
>>> type(i)
<class '__main__.MyInt'>
>>> j = 3
>>> i^j
8
If your list of operators is small enough you can probably use lambdas.
def operator_factory(op):
if op == '+':
return lambda x,y: x + y
elif op == '-':
return lambda x,y: x - y
elif op == '*':
return lambda x,y: x * y
elif op == '/':
return lambda x,y: x / y
elif op == '^':
return lambda x,y: x ^ y
Your if statements can depend on user input. Then you just use this like so:
>>> f = operator_factory('+')
>>> f(2,3)
5

How to add with tuples

I have pseudo-code like this:
if( b < a)
return (1,0)+foo(a-b,b)
I want to write it in python. But can python add tuples? What is the best way to code something like that?
I'd go for
>>> map(sum, zip((1, 2), (3, 4)))
[4, 6]
or, more naturally:
>>> numpy.array((1, 2)) + numpy.array((3, 4))
array([4, 6])
Do you want to do element-wise addition, or to append the tuples? By default python does
(1,2)+(3,4) = (1,2,3,4)
You could define your own as:
def myadd(x,y):
z = []
for i in range(len(x)):
z.append(x[i]+y[i])
return tuple(z)
Also, as #delnan's comment makes it clear, this is better written as
def myadd(xs,ys):
return tuple(x + y for x, y in izip(xs, ys))
or even more functionally:
myadd = lambda xs,ys: tuple(x + y for x, y in izip(xs, ys))
Then do
if( b < a) return myadd((1,0),foo(a-b,b))
tuple(map(operator.add, a, b))
In contrast to the answer by highBandWidth, this approach requires that the tuples be of the same length in Python 2.7 or earlier, instead raising a TypeError. In Python 3, map is slightly different, so that the result is a tuple of sums with length equal to the shorter of a and b.
If you want the truncation behavior in Python 2, you can replace map with itertools.imap:
tuple(itertools.imap(operator.add, a, b))
If you want + itself to act this way, you could subclass tuple and override the addition:
class mytup(tuple):
def __add__(self, other):
if len(self) != len(other):
return NotImplemented # or raise an error, whatever you prefer
else:
return mytup(x+y for x,y in izip(self,other))
The same goes for __sub__, __mul__, __div__, __gt__ (elementwise >) etc. More information on these special operators can be found e.g. here (numeric operations) and here (comparisions)
You can still append tuples by calling the original tuple addition: tuple.__add__(a,b) instead of a+b. Or define an append() function in the new class to do this.

Python 3.x - Simple function that returns another function

I just recently started programming in Python and I've been trying to create a simple function that takes two parameters, a and b, and returns the result of the sum of a and |b|. I want to return f(a, b) and not just f. I know that I'm assigning f to be an int in my current code and so it returns "int not callable error" when I run. So I have to assign f to be a function of some sort. I'm fairly certain I'm missing something fundamental here, I'm just not sure exactly what. Thanks for any help!
from operator import add, sub
def a_plus_abs_b(a, b):
"""Return a+abs(b), but without calling abs.
>>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
"""
if b < 0:
f = sub(a, b)
else:
f = add(a, b)
return f(a, b)
f = sub(a, b)
doesn't create a function it computes the result, so when you're calling f(a, b) you're calling an integer.
To fix it, assign the function in-line with a ternary to select which function to create depending on the sign of b
f = sub if b < 0 else add
Jean-Fançois's answer is great, but if you're not understanding the fundamentals behind this, you should look into another example that uses lambdas:
def returns_function_example():
return lambda arg1: arg1 + 2
function_returned = returns_function_example()
print(function_returned(1))
# Output = 3
Run this in your debugger to see that "function_returned" in indeed a function. You can connect the dots after the "print" function is called.
Functions are first-class citizens in Pythonland, so that you can manipulate them as any other object.
Suppose you create your function:
def add(a, b): return a + b
If you write add, that's a function. But if you write add(2,4), or add(a, b) assuming that a and b are defined, then you are calling the function, so you get a result. These are two completely different things: there is f, a callable, and f(a,b) which returns a value.
So if you write:
>>> f = add
>>> type(f)
<class 'function'>
That's what you want, you now have a function that does exactly what add does (and you could say it actually is add).
By contrast, if you write:
>>> f = add(a,b)
>>> type(f)
<class 'int'>
>>> f
11
That's a value.
So to make a long story short:
from operator import add, sub
def a_plus_abs_b(a, b):
"""
Return a+abs(b), but without calling abs.
>>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
"""
if b < 0:
f = sub
else:
f = add
return f(a, b)

Categories

Resources