python 是否存在限制 key 的 dict
迷茫
迷茫 2017-04-18 09:40:44
0
3
287

RT

python 是否存在不能改变 key 的 dict?
dict 的 keys 在定义的时候就规定

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (3)
伊谢尔伦

Rewrite it yourself

Override__setitem____delitem__method

    小葫芦

    I didn’t understand what the poster meant. The key of the dict itself cannot be modified directly, unless it is deleted first and then added. The key can be defined when creating a new dict

    dict = {'k1' : 1, 'k2' : 2, 'k3' : 3}
      Ty80

      Using__slots__属性。
      __slots__in an object will limit the object to only use the specified properties.

      >>> class SLT: __slots__ = ['a','b','c'] >>> slt=SLT() >>> slt.a=0 >>> slt.b Traceback (most recent call last): File "", line 1, in  slt.b AttributeError: b >>> slt.b=99 >>> slt.b 99 >>> slt.d=111 Traceback (most recent call last): File "", line 1, in  slt.d=111 AttributeError: 'SLT' object has no attribute 'd' >>> slt.x='xxx' Traceback (most recent call last): File "", line 1, in  slt.x='xxx' AttributeError: 'SLT' object has no attribute 'x'
        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!