Python: defining my own operators?

PythonOperators

Python Problem Overview


I would like to define my own operator. Does python support such a thing?

Python Solutions


Solution 1 - Python

While technically you cannot define new operators in Python, this clever hack works around this limitation. It allows you to define infix operators like this:

# simple multiplication
x=Infix(lambda x,y: x*y)
print 2 |x| 4
# => 8

# class checking
isa=Infix(lambda x,y: x.__class__==y.__class__)
print [1,2,3] |isa| []
print [1,2,3] <<isa>> []
# => True

Solution 2 - Python

No, Python comes with a predefined, yet overridable, set of operators.

Solution 3 - Python

No, you can't create new operators. However, if you are just evaluating expressions, you could process the string yourself and calculate the results of the new operators.

Solution 4 - Python

Sage provides this functionality, essentially using the "clever hack" described by @Ayman Hourieh, but incorporated into a module as a decorator to give a cleaner appearance and additional functionality – you can choose the operator to overload and therefore the order of evaluation.

from sage.misc.decorators import infix_operator

@infix_operator('multiply')
def dot(a,b):
    return a.dot_product(b)
u=vector([1,2,3])
v=vector([5,4,3])
print(u *dot* v)
# => 22

@infix_operator('or')
def plus(x,y):
    return x*y
print(2 |plus| 4)
# => 6

See the Sage documentation and this enhancement tracking ticket for more information.

Solution 5 - Python

Python 3.5 introduces the symbol @ for an extra operator.

PEP465 introduced this new operator for matrix multiplication, to simplify the notation of many numerical code. The operator will not be implemented for all types, but just for arrays-like-objects.

You can support the operator for your classes/objects by implementing __matmul__().

The PEP leaves space for a different usage of the operator for non-arrays-like objects.

Of course you can implement with @ any sort of operation different from matrix multiplication also for arrays-like objects, but the user experience will be affected, because everybody will expect your data type to behave in a different way.

Solution 6 - Python

If you intend to apply the operation on a particular class of objects, you could just override the operator that matches your function the closest... for instance, overriding __eq__() will override the == operator to return whatever you want. This works for almost all the operators.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncwjView Question on Stackoverflow
Solution 1 - PythonAyman HouriehView Answer on Stackoverflow
Solution 2 - PythondfaView Answer on Stackoverflow
Solution 3 - PythonZifreView Answer on Stackoverflow
Solution 4 - PythonbillyjmcView Answer on Stackoverflow
Solution 5 - Pythongg349View Answer on Stackoverflow
Solution 6 - PythonSudhir JonathanView Answer on Stackoverflow