Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax rule for having trailing commas in tuple definitions?

People also ask

What is a trailing comma in Python?

Description: In Python, a tuple is actually created by the comma symbol, not by the parentheses. Unfortunately, one can actually create a tuple by misplacing a trailing comma, which can lead to potential weird bugs in your code. You should always use parentheses explicitly for creating a tuple.

Which is the correct syntax of tuple?

In Python, tuple object is an immutable collection of items, not necessarily of same type. Items are separated by comma and placed inside parentheses, although they are optional. If only one item is to be included in a tuple, an additional comma is placed after it.

Does Python allow trailing commas?

Python Language Dictionary The trailing comma Like lists and tuples, you can include a trailing comma in your dictionary. PEP 8 dictates that you should leave a space between the trailing comma and the closing brace.

Why does a tuple need a comma?

The reason why you need a comma , for a tuple with one element is that "tuple is an object delimited by comma , ", not "an object enclosed in parentheses () ". Note that it is actually the comma which makes a tuple, not the parentheses.


It is only required for single-item tuples to disambiguate defining a tuple or an expression surrounded by parentheses.

(1)  # the number 1 (the parentheses are wrapping the expression `1`)
(1,) # a 1-tuple holding a number 1

For more than one item, it is no longer necessary since it is perfectly clear it is a tuple. However, the trailing comma is allowed to make defining them using multiple lines easier. You could add to the end or rearrange items without breaking the syntax because you left out a comma on accident.

e.g.,

someBigTuple = (
                   0,
                   1,
                   2,
                   3,
                   4,
                   5,
                   6,
                   7,
                   8,
                   9,
                   10,
                   #...
                   10000000000,
               )

Note that this applies to other collections (e.g., lists and dictionaries) too and not just tuples.


In all cases except the empty tuple the comma is the important thing. Parentheses are only required when required for other syntactic reasons: to distinguish a tuple from a set of function arguments, operator precedence, or to allow line breaks.

The trailing comma for tuples, lists, or function arguments is good style especially when you have a long initialisation that is split over multiple lines. If you always include a trailing comma then you won't add another line to the end expecting to add another element and instead just creating a valid expression:

a = [
   "a",
   "b"
   "c"
]

Assuming that started as a 2 element list that was later extended it has gone wrong in a perhaps not immediately obvious way. Always include the trailing comma and you avoid that trap.


Another advantage of trailing commas is that it makes diffs look nicer. If you started with

a = [
    1,
    2,
    3
]

and changed it to

a = [
    1,
    2,
    3,
    4
]

The diff would look like

 a = [
     1,
     2,
-    3
+    3,
+    4
 ]

Whereas if you had started with a trailing comma, like

a = [
    1,
    2,
    3,
]

Then the diff would just be

 a = [
     1,
     2,
     3,
+    4,
 ]

It's optional: see the Python wiki.

Summary: single-element tuples need a trailing comma, but it's optional for multiple-element tuples.


Also, consider the situation where you want:

>>> (('x','y'))*4                         # same as ('x','y')*4
('x', 'y', 'x', 'y', 'x', 'y', 'x', 'y')
#Expected = (('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))

So in this case the outer parentheses are nothing more than grouping parentheses. To make them tuple you need to add a trailing comma. i.e.

>>> (('x','y'),)*4 
(('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))