Question about idioms for clearing a list

Tim Hochberg tim.hochberg at ieee.org
Tue Feb 7 20:25:02 EST 2006


bonono at gmail.com wrote:
> Fredrik Lundh wrote:
> 
>>>Python now has, what, three built-in mutable collections types:
>>>lists, dictionaries, and sets. Dicts and sets both have a clear()
>>>method and lists do not.
>>
>>dicts and sets are mappings, and lists are not.  mappings don't
>>support slicing.  lists do.
> 
> 
> I am confused. Could you explain this ? I was under the impression said
> above(mapping don't support slicing), until after I read the language
> reference. I don't think it is slicing as in the list slicing sense but
> it does use the term "extend slicing".
> 
> http://www.python.org/doc/2.4.2/ref/slicings.html
> 
> "The semantics for an extended slicing are as follows. The primary must
> evaluate to a mapping object, and it is indexed with a key that is
> constructed from the slice list, as follows. If the slice list contains
> at least one comma, the key is a tuple containing the conversion of the
> slice items; otherwise, the conversion of the lone slice item is the
> key. The conversion of a slice item that is an expression is that
> expression. The conversion of an ellipsis slice item is the built-in
> Ellipsis object. The conversion of a proper slice is a slice object
> (see section 3.2) whose start, stop and step attributes are the values
> of the expressions given as lower bound, upper bound and stride,
> respectively, substituting None for missing expressions."


This is in place to support multidimensional arrays, such as in numpy. 
If, for instance, you have a 9x9 array A, then A[3:6,3:6] will extract a 
3x3 block from the center of it. A[3:6,3:6] is equivalent to 
A[(slice(3,6,None), slice(3,6,None))] and the resulting tuple gets 
passed through the mapping interface, but it is not a mapping in any 
real sense.

I don't think there's anything in core Python that uses this and it's 
not really relevant to this thread.

-tim




More information about the Python-list mailing list