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,) tuple(['a']) ('a',) tuple(tuple([1])) (1,) tuple([testclass(),1,'a'])[1:2] (1,) (1,2,testclass())[2:] (a,) #no matter what way you get it as lon as its a single element it includes the , after the element ... #however with multiple elements ... tuple([1,2]) (1, 2) (1,2) (1, 2) (1,2,3)[1:] (2, 3)
Christian Yeganeh
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.
participants (2)
-
Christian Yeganeh
-
Georg Brandl