0

python (2.7 in my case) has a several good looking ways to define tuples:

>>> a = (1,2,3)
>>> a
(1, 2, 3)
>>> a = 1,2,3
>>> a
(1, 2, 3)

But i don't understand why you can't define tuples with "empty" values, that you can't even access from index!

>>> a = 1,
>>> a
(1,)
>>> a[0]
1
>>> a[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

This behavior seems to be really error-prone. The second value of the tuple is "nothing", and it's not even None!

>>> a = 1,None
>>> a
(1, None)

I don't get the point of it, and it really feels like a bug. Can someone explain this behavior?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sebastien
  • 682
  • 1
  • 14
  • 26
  • 2
    No, there is *no second value*. The tuple contains only 1 item. Did you want your tuple to return 'something' for non-existing indices? – Martijn Pieters Jan 30 '14 at 12:27
  • 1
    Are you asking why you need to use at least one comma to define a tuple instead? – Martijn Pieters Jan 30 '14 at 12:27
  • 3
    `a = 1` is a variable containing one, the `a = (1)` is the same. Only `a = (1,)` creates a tuple with a single value. – RedX Jan 30 '14 at 12:28
  • 1
    Did you read [this section of tutorial](http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences)? – Janne Karila Jan 30 '14 at 12:29
  • Is your confusion that there is a comma after the last item in the tuple as printed? That has always bothered me too - looks like lazy programming but there is probably a deeper reason. @MartijnPieters do you have any insights into that? – Floris Jan 30 '14 at 12:29
  • 3
    When defining a tuple, the `()` parenthesis are optional in many contexts. It is the *comma* that defines a tuple instead. But to create a tuple with one element, you need at least one comma. There is no second element there. – Martijn Pieters Jan 30 '14 at 12:29
  • I've been debugging for almost half an hour to end up finding an extra comma in the code, transforming a string into a "1-item tuple". What's the point of defining a 1-item tuple? Why can we do that? – Sebastien Jan 30 '14 at 12:29
  • it's just a notation, don't take it literary – Karoly Horvath Jan 30 '14 at 12:29
  • 5
    @Floris: The grammar allows for a trailing comma, because that makes maintenance of sequences with constants **much** easier, as well as making diffs between edits clearer (just add another line with `newvalue,` instead of having to tack on the missing comma on the preceding line too). – Martijn Pieters Jan 30 '14 at 12:31
  • @Sebastien: http://stackoverflow.com/questions/4828041/are-there-any-uses-of-the-empty-tuple-in-python – Karoly Horvath Jan 30 '14 at 12:31
  • @Sebastien: Because `(1)` is not a tuple. It is an expression in parenthesis. – Martijn Pieters Jan 30 '14 at 12:31
  • @MartijnPieters - as usual you came through. Thanks! – Floris Jan 30 '14 at 12:32
  • @KarolyHorvath: thx, you helped me get why empty tuples and one item tuples are usefull. Making recursive algorithms possible on tuples is indeed a very good reason. – Sebastien Jan 30 '14 at 12:39
  • @MartijnPieters: ok, i get this and i agree with you. But then, i think that the tuple definition syntax without parenthesis is error-prone and shouldn't be accepted by python since it's not necessary. – Sebastien Jan 30 '14 at 12:41
  • @Sebastien: feel free to suggest that as a change in Python 4, but they're not going away in Python 2 or 3. – Wooble Jan 30 '14 at 12:44
  • it's a dynamic language. that's why you do unit tests. – Karoly Horvath Jan 30 '14 at 12:44
  • 1
    @Sebastien: it is fantastically useful, and most people use it all the time without realizing it. Ever swapped variables with `a, b = b, a`? – Martijn Pieters Jan 30 '14 at 12:45
  • @MartijnPieters: (a,b)=(b,a) still works! Well, the question is going off-topic, but IMO, we should forbid the other syntax to detect more human mistakes while programming. – Sebastien Jan 30 '14 at 12:54
  • @Sebastien: Really, the mistake happens rarely enough. And Python syntax is not going to change that dramatically unless there is going to be a Python 4 (not likely anytime soon). – Martijn Pieters Jan 30 '14 at 12:57
  • Because `(1)` is an integer not a tuple. You have to write it with a comma `(1,)` so it becomes a tuple. –  Jan 30 '14 at 14:12

0 Answers0