Is it reasonable to use None as a dictionary key in Python?

前端 未结 6 2196
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 20:15

None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works:

>>> x={\'a\':1, \'b\':2, N         


        
6条回答
  •  囚心锁ツ
    2021-02-06 20:59

    You want trouble? here we go:

    >>> json.loads(json.dumps({None:None}))
    {u'null': None}
    

    So yea, better stay away from json if you do use None as a key. You can patch this by custom (de/)serializer, but I would advise against use of None as a key in the first place.

提交回复
热议问题