[Python-ideas] Dict literal use for custom dict classes

Paul Moore p.f.moore at gmail.com
Tue Dec 15 14:02:43 EST 2015


On 13 December 2015 at 00:53, Joseph Jevnik <joejev at gmail.com> wrote:
> One thing that might make the association lists more readable without
> changing the language would be to visually break up the pairs over multiple
> lines. This could change the `OrderedDict` construction to look like:
>
> OrderedDict([
>     (k0, v0),
>     (k1, v1),
>     (kn, vn),
> ])

This is (IMO) readable, but a bit heavy on punctuation. The OP suggested

    OrderedDict{1: 'a', 4: int, 2: (3, 3)}

as a syntax - while it's a bit too special case on its own, one
possibility would be to have

    callable{k1: v1, k2: v2, ...}

be syntactic sugar for

    callable([(k1, k1), (k2, v2), ...])

Then the syntax would work with any function or constructor that took
"list of key/value pairs" as an argument.

Points against this suggestion, however:

1. It's not clear to me if this would be parseable within the
constraints of the Python language parser.
2. It is *only* syntax sugar, and as such adds no extra expressiveness
to the language.
3. It's still pretty specialised - while the "list of key/value pairs"
pattern is not uncommon, it's not exactly common, either...

Paul


More information about the Python-ideas mailing list