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

前端 未结 10 1463
被撕碎了的回忆
被撕碎了的回忆 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:18

    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,
     ]
    
    0 讨论(0)
  • 2020-11-22 06:18

    Trailing comma is required for one-element tuples only. Having a trailing comma for larger tuples is a matter of style and is not required. Its greatest advantage is clean diff on files with multi-line large tuples that are often modified (e.g. configuration tuples).

    0 讨论(0)
  • 2020-11-22 06:20

    That's a simple answer.

    a = ("s") is a string

    and

    a = ("s",) is a tuple with one element.

    Python needs an additional comma in case of one element tuple to, differentiate between string and tuple.

    For example try this on python console:

    a = ("s")

    a = a + (1,2,3)

    Traceback (most recent call last):

    File stdin, line 1, in module

    TypeError: cannot concatenate 'str' and 'tuple' objects

    0 讨论(0)
  • 2020-11-22 06:20

    PEP 8 -- Style Guide for Python Code - When to Use Trailing Commas

    Trailing commas are usually optional, except they are mandatory when making a tuple of one element (and in Python 2 they have semantics for the print statement). For clarity, it is recommended to surround the latter in (technically redundant) parentheses.

    Yes:

    FILES = ('setup.cfg',)
    

    OK, but confusing:

    FILES = 'setup.cfg',
    

    When trailing commas are redundant, they are often helpful when a version control system is used, when a list of values, arguments or imported items is expected to be extended over time. The pattern is to put each value (etc.) on a line by itself, always adding a trailing comma, and add the close parenthesis/bracket/brace on the next line. However it does not make sense to have a trailing comma on the same line as the closing delimiter (except in the above case of singleton tuples).

    Yes:

    FILES = [
        'setup.cfg',
        'tox.ini',
        ]
    initialize(FILES,
               error=True,
               )
    

    No:

    FILES = ['setup.cfg', 'tox.ini',]
    initialize(FILES, error=True,)
    
    0 讨论(0)
  • 2020-11-22 06:21

    It's optional: see the Python wiki.

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

    0 讨论(0)
  • 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'))
    
    0 讨论(0)
提交回复
热议问题