断言错误消息相当于Python中的错误消息

Ore*_*lom 0 python assert

有没有像 C/CPP 中那样使用 pythonic 方式编写带有消息的断言:

assert(i <= j && "more participants than medals");
Run Code Online (Sandbox Code Playgroud)

当我尝试等效方法时,我收到 pylint 错误,这可能表明有更好的方法(?):

R1726: Boolean condition 'i <= j and "..."' may be simplified to 'i <= j' (simplifiable-condition)
Run Code Online (Sandbox Code Playgroud)

小智 7

在 python 中你应该使用这种格式:

assert <condition>,<error message>
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下它必须是这样的:

assert i <= j,"more participants than medals"
Run Code Online (Sandbox Code Playgroud)