制作xor-tes的Pythonic方法

2024-03-29 11:02:45 发布

您现在位置:Python中文网/ 问答频道 /正文

什么是pythonic方法来编写检查两个变量不能都是none,并且两个变量也不能都不是none的条件。例如

if a is None and b is None: raise SystemExit(1)
if a is not None and b is not None: raise SystemExit(1)
# rest of the code

Tags: andofthe方法nonerestifis
2条回答
if (a is None) != (b is None):
    raise SystemExit("kkthxbye")
# remainder of code

What's the difference between XOR and NOT-EQUAL-TO?

你可以试试if all((a,b is not None)): pass

In [31]: a = 1

In [32]: b = 1

In [33]: all((a,b is not None))
Out[33]: True

In [34]: b = None

In [35]: all((a,b is not None))
Out[35]: False

注意:ab的值设置为0将产生True

Python?

In [36]: len('if a is not None and b is not None')
Out[36]: 34

In [37]: len('all((a,b is not None))')
Out[37]: 22

相关问题 更多 >