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

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

    Another reason that this exists is that it makes code generation and __repr__ functions easier to write. For example, if you have some object that is built like obj(arg1, arg2, ..., argn), then you can just write obj.__repr__ as

    def __repr__(self):
        l = ['obj(']
        for arg in obj.args: # Suppose obj.args == (arg1, arg2, ..., argn)
            l.append(repr(arg))
            l.append(', ')
        l.append(')')
        return ''.join(l)
    

    If a trailing comma wasn't allowed, you would have to special case the last argument. In fact, you could write the above in one line using a list comprehension (I've written it out longer to make it easier to read). It wouldn't be so easy to do that if you had to special case the last term.

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

    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.

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

    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.

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

    Coding style is your taste, If you think coding standard matters there is a PEP-8 That can guide you.

    What do you think of the result of following expression?

    x = (3)
    x = (3+2)
    x = 2*(3+2)
    

    Yep, x is just an number.

    0 讨论(0)
提交回复
热议问题