一个用于 Python 的高性能不可变映射类型。
.. image:: https://github.com/MagicStack/immutables/workflows/Tests/badge.svg?branch=master
:target: https://github.com/MagicStack/immutables/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush
.. image:: https://img.shields.io/pypi/v/immutables.svg
:target: https://pypi.python.org/pypi/immutables
Python 的一个不可变映射类型。
底层数据结构是哈希数组映射树 (HAMT)
在 Clojure、Scala、Haskell 和其他函数式语言中使用。
此实现在 CPython 3.7 中用于 contextvars
模块(请参阅 PEP 550_ 和
请参阅 PEP 567 以了解更多详细信息)。
基于 HAMT 的不可变映射对于两者都具有 O(log N) 的性能
set() 和 get() 操作,对于大多数情况来说基本上是 O(1)
相对较小的映射。
以下是一个简单的 get/set 基准测试,比较 HAMT 和使用 Python dict copy-on-write 方法实现的不可变映射(基准代码可在这里 _):
print(map['a'])
print(map.get('z', 100))
print('z' in map)
由于 Maps 是不可变的,因此提供了特殊的 API 用于修改,允许对 Map 对象进行更改并创建新的 (派生) Maps:
map_mutation = map.mutate() map_mutation['a'] = 100 del map_mutation['b'] map_mutation.set('y', 'y') map2 = map_mutation.fin
暂无开放 Issues,或尚未同步最近议题。