Search code examples
pythonpython-3.xassert

Assert in python3


when going through some code, I found a line that confused me a little.

assert x.shape == y.shape,(x.shape, y.shape)

I know, that assert x.shape == y.shape is basically a safety check to make sure, x and y have the same shape (i.e. have the same dimensions) But what does the ,(x.shape, y.shape) behind it mean? What is it good for?


Solution

  • That (x.shape, y.shape) is the message to print with the assertion error. Your code would be equivalent of:

    if __debug__:
        if not x.shape == y.shape:
            raise AssertionError((x.shape, y.shape))
    

    __debug__ This constant is true if Python was not started with an -O option.