"Data blocks" syntax specification draft

Chris Angelico rosuav at gmail.com
Wed May 23 09:56:10 EDT 2018


On Wed, May 23, 2018 at 11:11 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Wed, 23 May 2018 11:10:33 +0100, bartc wrote:
>> 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.

We actually have THREE special cases and only two types that follow
the general case. Here's the general case:

List of three: [1, 2, 3] or [1, 2, 3,]
List of two: [1, 2] or [1, 2,]
List of one: [1] or [1,]
List of zero: []

Dict of three: {1:1, 2:2, 3:3} or {1:1, 2:2, 3:3,}
Dict of two: {1:1, 2:2} or {1:1, 2:2,}
Dict of one: {1:1} or {1:1,}
Dict of zero: {}

Perfect! Now let's try that with other types.

Tuple of three: 1, 2, 3 or 1, 2, 3,
Tuple of two: 1, 2 or 1, 2,
Tuple of one: 1, # no other way to do it
Tuple of zero: ()

Set of three: {1, 2, 3} or {1, 2, 3,}
Set of two: {1, 2} or {1, 2,}
Set of one: {1} or {1,}
Set of zero: set()

The empty set and empty tuple are special, as is the single-element
tuple (you can't omit the comma). So, yes, there are definitely
special cases in the grammar, and they come about because practicality
beats purity. If we wanted perfectly clean grammar with no special
cases, we'd probably have to use two-character bracketings, to ensure
that everything is uniquely spellable, and there'd be no omitting them
from tuples - so it really WOULD be the bracketing characters that
define a tuple. But what would we gain? Very little. A few less
special cases, maybe, in return for needing to write more verbose
syntax for every literal/display type.

ChrisA



More information about the Python-list mailing list