我试图理解一些 python 2.5 代码,我遇到了这种模式:
def __init__(self, matrix, top_buttons, side_buttons, config_button):
raise isinstance(matrix, ButtonMatrixElement) or AssertionError
raise matrix.width() == 8 and matrix.height() == 8 or AssertionError
raise isinstance(top_buttons, tuple) or AssertionError
raise len(top_buttons) == 8 or AssertionError
raise isinstance(side_buttons, tuple) or AssertionError
raise len(side_buttons) == 8 or AssertionError
raise isinstance(config_button, ButtonElement) or AssertionError
我尝试在 shell 中使用一些简单的条件语句进行测试,如下所示:
>>> str = 'hello'
>>> raise len(str) == 5 or AssertionError
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
raise len(str) == 5 or AssertionError
TypeError: exceptions must be classes, instances, or strings (deprecated), not bool
所以从这个测试来看,至少我试过的方式,你不能提出布尔语句。那么引发条件表达式是什么意思,为什么它在函数中__init__
起作用但在我的测试代码中不起作用?