不可变字典类。
项目描述
constantdict
Python的不可变字典类,作为Python内置dict
类的薄层实现。它通常比其他不可变字典实现更快。
用法
使用以下命令安装此包
$ pip install constantdict
用法示例
from constantdict import constantdict
cd = constantdict({1: 2})
# constantdicts compare equal to dicts with the same items
assert cd == {1: 2}
# constantdicts are hashable, and their hashes are cached
print(hash(cd), cd)
# Attempting to modify the constantdict raises an AttributeError
try:
# Similar for pop(), popitem(), clear(), __ior__(), del, and setdefault()
cd[4] = 12
except AttributeError:
pass
请参阅文档,以及examples/
目录中的示例。
参考文献
其他包
PEPs
许可证
MIT许可证。