冻结集就是冻结集。 冻结列表可以是元组。 冷冻字典会是什么?一个不可变的、可哈希的字典。
我猜它可能是collections.namedtuple之类的东西,但那更像是一个冻结的字典(一个半冻结的字典)。不是吗?
一个“frozendict”应该是一个冻结的字典,它应该有键,值,get等,并支持in, for等。
更新: 在这里:https://www.python.org/dev/peps/pep-0603
冻结集就是冻结集。 冻结列表可以是元组。 冷冻字典会是什么?一个不可变的、可哈希的字典。
我猜它可能是collections.namedtuple之类的东西,但那更像是一个冻结的字典(一个半冻结的字典)。不是吗?
一个“frozendict”应该是一个冻结的字典,它应该有键,值,get等,并支持in, for等。
更新: 在这里:https://www.python.org/dev/peps/pep-0603
当前回答
安装frozendict
pip install frozendict
使用它!
from frozendict import frozendict
def smth(param = frozendict({})):
pass
其他回答
Freeze实现了可哈希的、类型提示的冻结集合(dict、list和set),并将递归地冻结你给他们的数据(如果可能的话)。
pip install frz
用法:
from freeze import FDict
a_mutable_dict = {
"list": [1, 2],
"set": {3, 4},
}
a_frozen_dict = FDict(a_mutable_dict)
print(repr(a_frozen_dict))
# FDict: {'list': FList: (1, 2), 'set': FSet: {3, 4}}
假设字典的键和值本身是不可变的(例如字符串),那么:
>>> d
{'forever': 'atones', 'minks': 'cards', 'overhands': 'warranted',
'hardhearted': 'tartly', 'gradations': 'snorkeled'}
>>> t = tuple((k, d[k]) for k in sorted(d.keys()))
>>> hash(t)
1524953596
你可以使用utispie包装的冷冻液:
>>> from utilspie.collectionsutils import frozendict
>>> my_dict = frozendict({1: 3, 4: 5})
>>> my_dict # object of `frozendict` type
frozendict({1: 3, 4: 5})
# Hashable
>>> {my_dict: 4}
{frozendict({1: 3, 4: 5}): 4}
# Immutable
>>> my_dict[1] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mquadri/workspace/utilspie/utilspie/collectionsutils/collections_utils.py", line 44, in __setitem__
self.__setitem__.__name__, type(self).__name__))
AttributeError: You can not call '__setitem__()' for 'frozendict' object
根据文件:
frozendict(dict_obj):接受dict类型的obj并返回一个可哈希且不可变的dict
Python没有内置的frozendict类型。事实证明,这并不经常有用(尽管它仍然可能比frozenset更有用)。
需要这种类型的最常见原因是在记忆函数调用带有未知参数的函数时。存储dict(其中值是可哈希的)的可哈希等价对象的最常见解决方案是类似tuple(sorted(kwargs.items()))的东西。
这取决于排序是不是有点疯狂。Python不能肯定地保证排序会产生合理的结果。(但它不能承诺太多其他东西,所以不要太担心。)
你可以很容易地做一些类似字典的包装。它可能看起来像
import collections
class FrozenDict(collections.Mapping):
"""Don't forget the docstrings!!"""
def __init__(self, *args, **kwargs):
self._d = dict(*args, **kwargs)
self._hash = None
def __iter__(self):
return iter(self._d)
def __len__(self):
return len(self._d)
def __getitem__(self, key):
return self._d[key]
def __hash__(self):
# It would have been simpler and maybe more obvious to
# use hash(tuple(sorted(self._d.iteritems()))) from this discussion
# so far, but this solution is O(n). I don't know what kind of
# n we are going to run into, but sometimes it's hard to resist the
# urge to optimize when it will gain improved algorithmic performance.
if self._hash is None:
hash_ = 0
for pair in self.items():
hash_ ^= hash(pair)
self._hash = hash_
return self._hash
它应该工作得很好:
>>> x = FrozenDict(a=1, b=2)
>>> y = FrozenDict(a=1, b=2)
>>> x is y
False
>>> x == y
True
>>> x == {'a': 1, 'b': 2}
True
>>> d = {x: 'foo'}
>>> d[y]
'foo'
安装frozendict
pip install frozendict
使用它!
from frozendict import frozendict
def smth(param = frozendict({})):
pass