What are the rules regarding chaining of "==" and "!=" in Python
<p>This morning, I find myself writing something like:</p>

<pre><code>if (a == b == c):
  # do something
</code></pre>

<p>And was surprised that it gave me the expected result.</p>

<p>I thought it would behave as:</p>

<pre><code>if ((a == b) == c):
  # do something
</code></pre>

<p>But it obviously didn't. It seems Python is treating the first statement differently from the second, which is nice but I couldn't find any documentation or explanation regarding this.</p>

<p>I tested and got this:</p>

<pre><code>In [1]: 2 == 2 == 2
Out[1]: True

In [2]: (2 == 2) == 2
Out[2]: False
</code></pre>

<p>Would someone care to explain me what are the rules regarding such "chaining" of <code>==</code> (or <code>!=</code>) ?</p>

<p>Thank you very much.</p>
