This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Jiajun Huang
Recipients Jiajun Huang
Date 2017-01-08.02:02:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1483840932.91.0.717045884042.issue29200@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-01-08 02:02:12Jiajun Huangsetrecipients: + Jiajun Huang
2017-01-08 02:02:12Jiajun Huangsetmessageid: <1483840932.91.0.717045884042.issue29200@psf.upfronthosting.co.za>
2017-01-08 02:02:12Jiajun Huanglinkissue29200 messages
2017-01-08 02:02:11Jiajun Huangcreate