A Python gotcha: Stray comma at end of simple assignment
<p>I just spent the better part of a day finding a bug caused by a stray comma at the end of an assignment statement. The difficulty in finding my bug was exacerbated by a third-party callback library that was trapping exceptions but it made me wonder why Python (2.x) doesn't raise a syntax error instead of creating a tuple.  Consider the following</p>

<pre><code>&gt;&gt;&gt; a = 1,
&gt;&gt;&gt; a
(1,)
</code></pre>

<p>As you can see, the trailing comma creates a singleton tuple.  It's not a violation of the Python grammar (see <a href="http://docs.python.org/reference/expressions.html#grammar-token-expression_list" rel="nofollow">http://docs.python.org/reference/expressions.html#grammar-token-expression_list</a>) but it can certainly lead to some unexpected results, e.g. </p>

<pre><code>&gt;&gt;&gt; a == 1,
(False,)
</code></pre>

<p>vs</p>

<pre><code>&gt;&gt;&gt; (1,) == a
True
</code></pre>

<p>Although I now understand what's going on, I'm puzzled about why Python permits this syntax instead of requiring explicit parentheses to create a tuple. <strong>Is there some case where this behavior is necessary, or at least advantageous?</strong>  I've been programming almost exclusively in Python for the past 7 years and I've never needed to create a singleton this way. Python is, in most respects, a wonderfully readable and explicit language. This particular "feature" seems, well, un-Pythonic.  </p>
