Override the {…} notation so i get an OrderedDict() instead of a dict()?

后端 未结 7 659
野的像风
野的像风 2020-11-28 21:38

Update: dicts retaining insertion order is guaranteed for Python 3.7+

I want to use a .py file like a config file. So using the {.

7条回答
  •  孤城傲影
    2020-11-28 21:53

    As of python 3.6, all dictionaries will be ordered by default. For now, this is an implementation detail of dict and should not be relied upon, but it will likely become standard after v3.6.

    Insertion order is always preserved in the new dict implementation:

    >>>x = {'a': 1, 'b':2, 'c':3 }
    >>>list(x.keys())
    ['a', 'b', 'c']
    

    As of python 3.6 **kwargs order [PEP468] and class attribute order [PEP520] are preserved. The new compact, ordered dictionary implementation is used to implement the ordering for both of these.

提交回复
热议问题