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

后端 未结 7 671
野的像风
野的像风 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 21:59

    What you are asking for is impossible, but if a config file in JSON syntax is sufficient you can do something similar with the json module:

    >>> import json, collections
    >>> d = json.JSONDecoder(object_pairs_hook = collections.OrderedDict)
    >>> d.decode('{"a":5,"b":6}')
    OrderedDict([(u'a', 5), (u'b', 6)])
    

提交回复
热议问题