python chain operation

Chain operation, simple and efficient, that is, directly calling functions

from operator import (add, sub)
def add_or_sub(a, b, oper):
        return (add if oper == '+' else sub)(a, b)
add_or_sub(1, 2, '-')      # -1
add_or_sub(2, 1, '-')      # 1
add_or_sub(2, 2, '+')      # 4

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/107487254