[Python-ideas] Extension of python/json syntax to support explicitly sets and ordered dict.

Steven D'Aprano steve at pearwood.info
Thu Sep 7 07:54:15 EDT 2017


On Thu, Sep 07, 2017 at 01:36:31PM +0200, Matteo Nastasi wrote:

> A set could be defined as { item1, item2, item3[...] }

Guido's time machine strikes again. Python 3:

py> s = {1, 2, 3}
py> type(s)
<class 'set'>


>     with {,} as an empty set

That looks like a typo. I don't think that having a literal for empty 
sets is important enough that we need worry about the lack.


> An ordered dict could be defined as [ item1: value1, item2: value2 ... ]
>     with [:] ase an empty odered dict

I think that's been proposed before.

I don't hate that suggestion, but I don't think its very useful either. 
We already have a de facto "Ordered Mapping" literal that can be passed 
to the OrderedDict constructor:

OrderedDict(
    [(key1, value1), (key2, value2), (key3, value3), ...]
    )

Its not quite as compact, but it isn't too awful. And if you really 
don't like it, try:

OrderedDict(zip(keys, values))



-- 
Steven


More information about the Python-ideas mailing list