What Is the Syntax Rule For Having Trailing Commas in Tuple Definitions

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

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.

Why does adding a trailing comma after a variable name make it a tuple?

It is the commas, not the parentheses, which are significant. The Python tutorial says:

A tuple consists of a number of values separated by commas

Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.

See the Python Tutorial section on Tuples and Sequences

Why are trailing commas allowed in a list?

The main advantages are that it makes multi-line lists easier to edit and that it reduces clutter in diffs.

Changing:

s = ['manny',
'mo',
'jack',
]

to:

s = ['manny',
'mo',
'jack',
'roger',
]

involves only a one-line change in the diff:

  s = ['manny',
'mo',
'jack',
+ 'roger',
]

This beats the more confusing multi-line diff when the trailing comma was omitted:

  s = ['manny',
'mo',
- 'jack'
+ 'jack',
+ 'roger'
]

The latter diff makes it harder to see that only one line was added and that the other line didn't change content.

It also reduces the risk of doing this:

s = ['manny',
'mo',
'jack'
'roger' # Added this line, but forgot to add a comma on the previous line
]

and triggering implicit string literal concatenation, producing s = ['manny', 'mo', 'jackroger'] instead of the intended result.

Why is a trailing comma needed here to pass a list in an *args parameter?

Because the comma makes the tuple, or so the saying goes.

(1) is just grouping parens, but (1, ) is a tuple with one int. In this case under the hood the thread initializer is unpacking args, so without the comma it's unpacking test list, basically test_func(*test_list) but with the comma it unpacks the tuple, and everything works.

Python tuples, what does , in the end mean

(0) wouldn't be interpreted by Python as a tuple, but instead as a numeric expression (like (1+2) except without any math operation). The trailing comma is used to tell Python that it is explicitly a 1-element tuple.

>>> type((0))
<type 'int'>
>>> type((0,))
<type 'tuple'>

(0) evaluates to a number:

>>> (0) == 0
True

(0,) evaluates to a tuple, which is not a number...

(0,) == 0
False

...but is a tuple.

>>> (0,) == tuple([0])
True

This isn't specific to numbers, either - (expression) will always be equivalent to expression, while (expression,) will always be a single-element tuple with the first (and only) item in the tuple being the result of expression.

Why the result of the function is different when one argument used in args?

Adding parentheses around a single item is the same as redundant grouping, and does not create the object as a tuple. From the docs:

A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).

The docs close with commenting that this implementation is indeed, "Ugly, but effective."

variable positional and printing the values

The first output shows a comma because without it, 1 being the only element, (1) would be just a integer (parentheses are wrapping the expression 1), (1,) is shown to differentiate tuples and simple parentheses.

in the second one, no trailing comma is needed to differentiate tuples, since there are more than one element.

In the third O/p, you are not passing 1 and 2, but instead you're passing the whole (1,2), so it shows only one item (which is (1,2)) in a tuple, and adds an extra comma. Same for the fourth: your passing the entire ((1,2), (3,4)).

How does Python interpret a half 'empty' tuple (x,)?

(2,) is literal for tuple with one element. You need that in

pos = np.empty(X.shape+(2,))

as X.shape is tuple and + denotes concatenating tuples in python, in this particular example you are adding another dimension before using numpy.empty.

Tuples and Sequence docs put it following way

A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses). Ugly, but
effective.



Related Topics



Leave a reply



Submit