Suggestion: make sequence and map interfaces more similar

Marko Rauhamaa marko at pacujo.net
Thu Mar 31 10:13:00 EDT 2016


Random832 <random832 at fastmail.com>:

> So can we discuss how a unified method to get a set of all valid
> subscripts (and/or subscript-value pairs) on an object would be a
> useful thing to have without getting bogged down in theoretical
> claptrap about the meaning of the mapping contract?

One could compose a table of correspondences:

      -------------------------------------------
      list (L)                    dict (D)       
      -------------------------------------------
      L[key] = value              D[key] = value
      del L[key] (*)              del L[key]         
      key >= 0 and key < len(L)   key in D
      range(len(L))               iter(D)
      L.clear                     D.clear
      L.copy                      D.copy
      lambda key: L[key]          D.get
      lambda: enumerate(L)        D.items
      lambda: range(len(L))       D.keys
             ...                    ...
      -------------------------------------------
      (*) reassigns all keys


Marko



More information about the Python-list mailing list