Skip to content

x == y == z #25993

Answered by jakevdp
cool-RR asked this question in Q&A
Jan 20, 2025 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

You can do this:

(x == y) & (y == z)

Note that the parentheses are required here due to operator precedence rules.

This fails because x == y just evaluates to booleans which are then compared to z.

Side note here, but it fails for a different reason: Python treats operator chaining like x == y == z as equivalent to (x == y) and (y == z), and the and keyword will try to cast the things on either side to bool, and bool(x == y) fails.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@cool-RR
Comment options

@cool-RR
Comment options

@jakevdp
Comment options

Answer selected by cool-RR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants