Mapping, with sequence as key, wildcard and subsequence matching

Terry Reedy tjreedy at
Thu Jul 16 07:27:36 CEST 2015

On 7/15/2015 9:51 PM, Ben Finney wrote:
> Howdy all,
> What well-defined data type exists with the following properties:
> * Mapping, key → value.
> * Each key is a sequence (e.g. `tuple`) of items such as text strings.
> * Items in a key may be the sentinel `ANY` value, which will match any
>    value at that position.
> * A key may specify that it will match *only* sequences of the same
>    length.
> * A key may specify that it will match sequences with arbitrarily many
>    additional unspecified items.

Every key should signal which of the last two alterntives holds. One can 
be a default.  The signal can be 'in-band', in the tuple key itself, or 
'out-of-band', not in the tuple key.  An in-band signal must be a 
special value, like 'ANY', that is not otherwise used in keys.  ... 
might be a good choice.  For out-of-band, you could try a tuple subclass 
with a __new__ method that sets an attribute.  Or wrap a tuple, say with 
a list, to signal the non-default alternative.  Something like

     tail = False
     if type(key) is list:
         tail = True
         key = key.pop()

Terry Jan Reedy

More information about the Python-list mailing list