1

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?

  • 1
    It's a message that gets printed with the assertion error, if one is raised. – AKX Apr 14 '21 at 06:33
  • See the part under *"To print a message if the assertion fails:"* in this Q&A: [What is the use of "assert" in Python?](https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python) – kaya3 Apr 14 '21 at 06:47

1 Answers1

2

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

if not x.shape == y.shape:
    raise AssertionError((x.shape, y.shape))
Giuppox
  • 1,393
  • 9
  • 35