84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
RT
python 是否存在不能改变 key 的 dict?dict 的 keys 在定义的时候就规定
业精于勤,荒于嬉;行成于思,毁于随。
Rewrite it yourself
Override__setitem____delitem__method
__setitem__
__delitem__
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}
Using__slots__属性。__slots__in an object will limit the object to only use the specified properties.
__slots__
>>> 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'
Rewrite it yourself
Override
__setitem__
__delitem__
methodI 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
Using
__slots__
属性。__slots__
in an object will limit the object to only use the specified properties.