assign operator to variable in python?

后端 未结 4 1410
一个人的身影
一个人的身影 2020-11-28 14:23

Usual method of applying mathematics to variables is

a * b

Is it able to calculate and manipulate two operands like this?

         


        
4条回答
  •  时光取名叫无心
    2020-11-28 14:56

    The operator module http://docs.python.org/library/operator.html exposes functions corresponding to practically all Python operators. You can map operator symbols to those functions to retrieve the proper function, then assign it to your op variable and compute op(a, b).

提交回复
热议问题