Python – How to Define Custom Operators operatorspython I would like to define my own operator. Does python support such a thing? Best Answer 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 Related SolutionsPython – Assign Operator to Variable You can use the operator module and a dictionary: import operator ops = { "+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.div } op_char = input('enter a operand') op_func = ops[op_char] result = op_func(a, b) Python Operators – Exploring New Operators in Python As @minitech said you can't define new operators. But check this hack that allows you to define infix operators http://code.activestate.com/recipes/384122-infix-operators/ Related Question
Best Answer
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: