Why did python choose commas over parenthesis in tuple design?

前端 未结 3 1768
你的背包
你的背包 2020-12-19 17:09

From python wiki

Multiple Element Tuples

In Python, multiple-element tuples look like:

1,2,3

3条回答
  •  既然无缘
    2020-12-19 17:44

    This is an artifact of the grammar.

    The terms separated by commas are a building block for tuples, lists, and sets depending on whether they are wrapped by square brackets, curly braces, or nothing at all.

    The chief challenge when specifying a grammar is balancing multiple competing uses of the same characters. Commas separate parts of lists, tuples, dicts, and sets. Commas also separate arguments in function calls. Trailing commas are allowed for both uses (and are required for tuples of length one). Likewise, parentheses have multiple uses including function calls and grouping for arithmetic expressions. The period serves as a decimal point and for the getattribute operator.

提交回复
热议问题