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

前端 未结 10 1480
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 05:54

In the case of a single element tuple, the trailing comma is required.

a = (\'foo\',)

What about a tuple with multiple elements? It seems t

10条回答
  •  被撕碎了的回忆
    2020-11-22 06:24

    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'))
    

提交回复
热议问题