2024-03-29 11:02:45 发布
网友
什么是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
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
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
注意: 将a或b的值设置为0将产生True
a
b
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
What's the difference between XOR and NOT-EQUAL-TO?
你可以试试
if all((a,b is not None)): pass
注意: 将
a
或b
的值设置为0
将产生True
Python?
相关问题 更多 >
编程相关推荐