Message284949
the class definition:
class _HashedSeq(list):
""" This class guarantees that hash() will be called no more than once
per element. This is important because the lru_cache() will hash
the key multiple times on a cache miss.
"""
__slots__ = 'hashvalue'
def __init__(self, tup, hash=hash):
self[:] = tup
self.hashvalue = hash(tup)
def __hash__(self):
return self.hashvalue
and I've test for it:
In [1]: from functools import _HashedSeq
In [2]: from unittest.mock import Mock
In [3]: test_tup = 1, 2, 3, "hello", "world"
In [4]: hash_func = Mock()
In [5]: _HashedSeq(test_tup, hash=hash_func)
Out[5]: [1, 2, 3, 'hello', 'world']
In [6]: _HashedSeq(test_tup, hash=hash_func)
Out[6]: [1, 2, 3, 'hello', 'world']
In [7]: _HashedSeq(test_tup, hash=hash_func)
Out[7]: [1, 2, 3, 'hello', 'world']
In [8]: hash_func.call_count
Out[8]: 3
the hash function had been called 3 times rather than 1. |
|
Date |
User |
Action |
Args |
2017-01-08 02:02:12 | Jiajun Huang | set | recipients:
+ Jiajun Huang |
2017-01-08 02:02:12 | Jiajun Huang | set | messageid: <1483840932.91.0.717045884042.issue29200@psf.upfronthosting.co.za> |
2017-01-08 02:02:12 | Jiajun Huang | link | issue29200 messages |
2017-01-08 02:02:11 | Jiajun Huang | create | |
|