在 python3 中断言

Assert in python3

在检查一些代码时,我发现了一行让我有点困惑。

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

我知道,assert x.shape == y.shape 基本上是一种安全检查,以确保 x 和 y 具有相同的形状(即具有相同的尺寸) 但是后面的,(x.shape, y.shape)是什么意思呢?它有什么用?

(x.shape, y.shape) 是要打印断言错误的消息。您的代码相当于:

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