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

前端 未结 6 2224
没有蜡笔的小新
没有蜡笔的小新 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:39

    jsonify does not support a dictionary with None key.

    From Flask import jsonify
    
    def json_():
        d = {None: 'None'}
        return jsonify(d)
    

    This will throw an error:
    TypeError: '<' not supported between instances of 'NoneType' and 'str'

提交回复
热议问题