python assert with and without parenthesis

后端 未结 5 918
南笙
南笙 2021-01-30 02:42

Here are four simple invocations of assert:

>>> assert 1==2
Traceback (most recent call last):
  File \"\", line 1, in ?
AssertionError

&g         


        
5条回答
  •  北海茫月
    2021-01-30 03:32

    You can break assert statement without \ like this:

    foo = 7
    assert foo == 8, (
        'derp should be 8, it is ' + str(foo))
    

    Or if you have even longer message:

    foo = 7
    assert foo == 8, (
        'Lorem Ipsum is simply dummy text of the printing and typesetting '
        'industry. Lorem Ipsum has been the industry\'s standard dummy text '
        'ever since the 1500s'
    )
    

提交回复
热议问题