def frozen(struct):
"""Return an immutable, hashable version of the given data structure.
Iterators (including generators) are hashable but mutable, so they
are evaluated and returned as tuples---if they are infinite, this
function will not exit.
"""
if isinstance(struct, Mapping):
return frozenset((k, frozen(v)) for k, v in struct.items())
if isinstance(struct, Set):
return frozenset(frozen(item) for item in struct)
if isinstance(struct, Iterable): # Includes iterators and generators
return tuple(frozen(item) for item in struct)
hash(struct) # Raise TypeError for unhashable objects
return struct
评论列表
文章目录