一成不变的词典
frozendict的Python项目详细描述
frozendict是实现 完整的映射接口。它可以作为 需要不变性的字典。
当然,这是python,您仍然可以在对象的 如果你愿意的话。
frozendict构造函数模拟dict,以及所有预期的 提供接口(iter,len,repr,hash,getitem)。 注意,frozendict不能保证其值的不变性,因此 hash方法的实用性受用法限制。
唯一的区别是frozendict的copy()方法 变量关键字参数,它将作为键/值对出现在新的, 不可变拷贝。
示例外壳用法:
fromfrozendictimportfrozendictfd=frozendict({'hello':'World'})printfd# <frozendict {'hello': 'World'}>printfd['hello']# 'World'printfd.copy(another='key/value')# <frozendict {'hello': 'World', 'another': 'key/value'}>