Mapping, with sequence as key, wildcard and subsequence matching

Ethan Furman ethan at
Thu Jul 16 08:31:08 CEST 2015

On 07/15/2015 10:53 PM, Ben Finney wrote:
> Steven D'Aprano <steve at> writes:

>> You can't use a dict for the mapping, not unless you're smarter than
>> me, due to the requirement to hash the keys.
> Dang. It's the mapping that I really need to solve, I think. A mapping
> that has a custom “does this candidate match any existing key” and
> “return the value for this key” to defer to the matching behaviour
> described above.
> Are those the ‘__contains__’, ‘__getitem__’ methods? What actually is
> the API of a mapping type, that would need to be customised for this
> application?

The problem is that potential key matches are found by hashes, and the hash of

   ('value1', ANY, 'next_value')


   ('value1, 'value2', 'next_value')


   ('value1', 'value3', 'next_value')

will not and cannot be the same.

If you fiddle with your object such that all instances hash to the same value then you lose the O(1) lookup for the dict -- you basically have a list.


More information about the Python-list mailing list