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

Franklin? Lee leewangzhong+python at gmail.com
Tue Dec 15 07:23:59 EST 2015


On Sun, Dec 13, 2015 at 12:15 AM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> And I think there's some precedent here. IIRC, in YAML, {1:2, 3:4} is unordered dict a la JSON (and Python), but [1:2, 3:4] is... actually, I think it's ambiguous between an ordered dict and a list of pairs, and you can resolve that by declaring !odict or !seq, or you can just leave it up to the implementation to pick one if you don't care... but let's pretend it wasn't ambiguous; either one covers the use case (and Python only has the latter option anyway, unless OrderedDict becomes a builtin).

For YAML, I read it as a list of dicts. My Python's yaml module
(pyyaml?) agrees.

>>> import yaml
>>> yaml.load('[a: 1, b: 2]')
[{'a': 1}, {'b': 2}]

However, YAML's website (page: http://www.yaml.org/refcard.html) lists
the !!omap type cast as using this syntax:

    '!!omap': [ one: 1, two: 2 ]

I tried using !!seq. Not sure if I'm doing it right.

>>> yaml.load('!!seq [a: 1, b: 2]')
[{'a': 1}, {'b': 2}]
>>> yaml.load('!!omap [a: 1, b: 2]')
[('a', 1), ('b', 2)]

(Huh. PyYAML module thinks that an omap should be a list of pairs. It
might eventually change to OrderedDict, though.)


More information about the Python-ideas mailing list