[docs] Bug report
Georg Brandl
georg at python.org
Wed May 4 01:21:18 EDT 2016
On 05/04/2016 01:51 AM, Christian Yeganeh wrote:
> I have found a bug. Its small and not harmful. But its still there. If you take the string of a tuple, it will not always return the correct string.
> If you have a tuple with only one element and cast it using str() it will put the comma at the end of the string before the ) as if there was a second element.
>
> sample code:
>>>> class testclass:
> ... def __repr__(s): return 'a'
> ...
>>>> (1,2)[1:]
> (2,)
>>>> tuple([1])
> (1,)
...
Hi Christian,
this is not a bug, but expected behavior: since 1-tuples are a valid type,
we have to distinguish them from simply parenthesized expressions by the
trailing comma. You can see that when trying to write a 1-tuple literal:
>>> (1)
1
>>> (1,)
(1,)
And the repr output just mirrors the input syntax.
cheers,
Georg
BTW, trailing commas are also allowed whenever you have a comma-separated
list in Python. Always using a trailing comma for line-by-line lists
helps in reducing diff churn and not forgetting the comma when adding
elements.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/docs/attachments/20160504/b4a46f80/attachment.sig>
More information about the docs
mailing list