"Data blocks" syntax specification draft
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed May 23 09:11:36 EDT 2018
On Wed, 23 May 2018 11:10:33 +0100, bartc wrote:
[...]
>> You haven't done enough testing. All you have done is found that "round
>> brackets give a tuple, other brackets don't". But you need to test what
>> happens if you take away the brackets to be sure that it is the round
>> brackets which create the tuple:
>>
>> a = 10, 20, 30 # take away the ()
>>
>> You still get a tuple. Taking away the [] and {} also give tuples.
>
> If ... is a comma-separated sequence of (2 or more) expressions, then:
>
> Enclosed with: It yields:
>
> {...} brackets Set (or dict with key:value items)
> [...] brackets List
> (...) brackets Tuple
> ... (no) brackets Tuple
Indeed.
> Each ... contains commas. But it is what surrounds ..., or that doesn't
> surround ..., that determines what the construction yields. The commas
> are not specific to tuples.
Nobody said that they were. You are arguing against a strawman. The
brackets are not part of tuple syntax. They are sometimes needed for
grouping, when some other grammatical construct would otherwise take
precedence. That is all.
>> What happens if you add extra brackets?
>>
>> a = ((10, 20, 30)) # tuple
>> b = ([10, 20, 30]) # list
>> c = ({10, 20, 30}) # set
>
> 0 items within the list:
>
> () Empty tuple
> [] Empty list
> {} Empty dict
Aye ... as we've acknowledged numerous times now, the empty tuple *is* a
genuine special case, one which *does* rely on an empty pair of round
brackets.
> 1 item within the list
>
> (x) Yields x
Exactly! There is no comma, therefore, there is no tuple.
> [x] List of one item
> {x} Set of one item
Neither of these require commas, since you don't need a separator to
separate a single item. The square brackets *do* specify that the item is
a list; the curly brackets *do* specify a dict or set. The round brackets
DO NOT specify a tuple (except in the empty tuple case).
No comma, no tuple, no matter how emphatically you insert round brackets
around the item.
> Because () is used for normal bracketing of expression terms, it
> requires this special form to denote a tuple:
>
> (x,) Tuple of one item
Incorrect. Yet again, you have failed to do enough testing. No special
form is required. Only a comma:
py> x = 1,
py> type(x)
<class 'tuple'>
It isn't enough to test examples which confirm a hypothesis. You need to
test examples which also refute it, and see if your hypothesis survives
the challenge.
Certainly commas are used as separators. They're used as separators in
many different contexts: import statements, function calls, function
declarations, class declarations, except statements, list displays, etc.
We don't dispute that or deny it.
But in the context of "tuple displays" (the official terminology for
tuple so-called "literal" syntax), the brackets are not part of the tuple
display. It is the commas that tell the interpreter that the result is a
tuple:
https://docs.python.org/3/reference/expressions.html#expression-lists
Because commas are used as separators in so many places, there is often a
clash between one part of the grammar that uses commas and their use in
tuples, requiring us to disambiguate the syntax with brackets.
https://docs.python.org/3/reference/expressions.html#parenthesized-forms
> which can ALSO be used to form one element lists, sets and dicts.
But *unlike* one-element lists, sets and dicts, where the comma is
optional, it is *required* for tuples.
[...]
> Suppose you were parsing Python source, had just processed a term of an
> expression, and the next token was a comma. What came before the term
> may have been (, {, [ or nothing. What comes next, would you call:
[...]
I don't even understand what point you think you are making here. But it
really doesn't matter. Ultimately, the grammar and documentation trumps
any hypotheses we might have about what the language is doing, and they
are absolutely clear about what is going on:
https://docs.python.org/3/reference/expressions.html#expression-lists
https://docs.python.org/3/reference/expressions.html#parenthesized-forms
--
Steve
More information about the Python-list
mailing list