如果条件动态变化? (蟒蛇)

时间:2016-09-20 14:30:43

标签: python function python-3.x if-statement

嘿伙计们,我想在动态函数中更改if条件。

def func(<):
     if y<x:
        return x

def func(=):
         if y=x:
            return x

我希望条件改变,任何想法?

1 个答案:

答案 0 :(得分:0)

您无法直接使用=<等运算符。但您可以从operator导入它们:

from operator import le, eq

def func(op):
    if op(x, y):
        return x

func(le)       # → x < y
func(eq)       # → x == y

BTW比较运算符为==而非=

相关问题