How to change the message in a Python AssertionError?

前端 未结 5 478
暖寄归人
暖寄归人 2020-12-07 23:57

I\'m writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the compa

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 00:54

    You want to take the caught exception, convert it to a string, combine it with some additional string info, and raise a new exception.

    x = 3
    y = 5
    try:
        assert( x == y )
    except AssertionError, e:
        raise( AssertionError( "Additional info. %s"%e ) )
    

提交回复
热议问题