summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSantiago Lezica <slezica89@gmail.com>2016-10-06 10:51:33 -0300
committerSantiago Lezica <slezica89@gmail.com>2016-10-06 10:51:33 -0300
commita26e07a4c4a823b08462e962d52b6f7f25598cd1 (patch)
treead9c8124f3c0bc7b5858d65344957eea8e194f6e
parentfc45de72eddd5244d06b8509cc4b529f078ad829 (diff)
parent86de7a2960f47e400f7e440394716b8a40d2968d (diff)
Merge branch 'perf-improvements' of https://github.com/eugene-eeo/python-frozendict into eugene-eeo-perf-improvements
-rw-r--r--frozendict/__init__.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/frozendict/__init__.py b/frozendict/__init__.py
index 1d77400..7ca64ea 100644
--- a/frozendict/__init__.py
+++ b/frozendict/__init__.py
@@ -10,6 +10,9 @@ except ImportError: # python < 2.7
OrderedDict = NotImplemented
+iteritems = getattr(dict, 'iteritems', dict.items) # py2-3 compatibility
+
+
class frozendict(collections.Mapping):
"""
An immutable wrapper around dictionaries that implements the complete :py:class:`collections.Mapping`
@@ -42,9 +45,10 @@ class frozendict(collections.Mapping):
def __hash__(self):
if self._hash is None:
- hashes = map(hash, self.items())
- self._hash = functools.reduce(operator.xor, hashes, 0)
-
+ h = 0
+ for key, value in iteritems(self._dict):
+ h ^= hash((key, value))
+ self._hash = h
return self._hash