[New-bugs-announce] [issue29200] is it a bug in `functools._HashedSeq`

Jiajun Huang report at bugs.python.org
Sat Jan 7 21:02:12 EST 2017

New submission from Jiajun Huang:

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.

components: Library (Lib)
messages: 284949
nosy: Jiajun Huang
priority: normal
severity: normal
status: open
title: is it a bug in `functools._HashedSeq`
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list