有没有办法在函数参数的 if
条件中指定函数?例如,我希望能够做这样的事情:
a = int(input('number between 0 and 5'))
b = >=
def fun1(a, b):
if a b 0:
print('...')
最佳答案
是的,如果您想使用比较运算符作为函数的参数,您可以使用operator
库中的方法——在这种情况下,您正在寻找operator.ge
。 :
import operator
a = int(input('number between 0 and 5'))
b = operator.ge
def fun1(a, b):
if b(a, 0):
print('...')
关于python - 是否可以将比较运算符作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71730346/