Is the walrus Operator antipattern?

Yeah, I just didn’t see how what appears to be a debugging check—that the user stated they don’t intend to run in production—is an inappropriate use of assert, given that is essentially its canonical purpose :slight_smile: However, using the walrus operator within an assert introduces the antipattern here (due to the risk of now or later relying on that assignment outside an assert, as your examples aptly demonstrate).

Precisely. It’s fine to assert, and it’s fine to have multiple assertions, and in theory it’s fine to have multiple assertions that depend on each other, but the risk is that non-assertion code will inadvertently depend on the assertion.

2 Likes