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

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

提交回复
热议问题