即使存在错误条件,Python Lambda也会返回true [英] Python lambda returns true even with a false condition

查看:59
本文介绍了即使存在错误条件,Python Lambda也会返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> if lambda x: True == True:
... print('yes')
...
yes
>>> if lambda x: False == True:
... print('yes')
...
yes

推荐答案

您不会像我在评论中解释的那样调用lambda,但是如果要内联,则可以按以下方式进行测试:

You're not calling your lambda as I've explained in my comment, but if you want it inline you can test as:

if (lambda x: True == True)(None):
    print("yes")  # prints

if (lambda x: False == True)(None):
    print("yes")  # doesn't print

或更笼统,因为您实际上没有在lambda中使用任何参数:

Or more general, since you're not actually using any arguments in your lambdas:

if (lambda: True == True)():
    print("yes")  # prints

if (lambda: False == True)():
    print("yes")  # doesn't print

这篇关于即使存在错误条件,Python Lambda也会返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆