+ 1

Python create new operator

theres a lot of math operators. like ** is pow. how to create the new one like *** ? i cant find the answer in internet

16th Feb 2018, 6:28 AM
Kevin AS
Kevin AS - avatar
2 Answers
+ 2
Unfortunately python can't define new operators... You can overload the current operators in a class with magic functions. Maybe the lambda is also useful for you : mul = (lambda x y: x * y ) mul 3 2 # gives 6 Or even with a little hack: mul = Infix(lambda x y: x * y ) 3 |mul| 2 # gives 6 Source how to get Infix working: http://code.activestate.com/recipes/384122/ Similar stack overflow question : https://stackoverflow.com/questions/932328/python-defining-my-own-operators
16th Feb 2018, 7:12 AM
Chrizzhigh
Chrizzhigh - avatar
0
Stack overflow is in general a good website for those kind of questions
16th Feb 2018, 7:15 AM
Chrizzhigh
Chrizzhigh - avatar