Featured image of post Python小技巧 - 使用不同的方式同时测试多个标记

Python小技巧 - 使用不同的方式同时测试多个标记

如何使用不同的方式同时测试多个标记

比如我有个需求如下

代码逻辑是这样的


x, y, z = 1, 0, 1
if x == 1 or y == 1 or z == 1:
    print('passed')

可以修改为如下方式,实现同时测试多个标记,不需要分开写,比较方便


if 1 in (x, y, z):
    print('passed')

再比如我有这个需求,只判断true 或者 false


if a or b or c:
    print('passed')

可以修改为下面的方式,实现同时测试多个标记


if any((a, b, c)):
    print('passed')

每天积累一点,技术成熟一点