slicing, mapping types, ellipsis etc.

Kent Johnson kent3737 at yahoo.com
Tue Nov 30 09:26:34 EST 2004


Nick Coghlan wrote:
> Kent Johnson wrote:
> 
>> Maybe instead of referring to mappings it should say "The primary must 
>> implement __getitem__(), which is called with a value that is 
>> constructed from the slice list, as follows."
> 
> 
> In the section you mention, 'mapping' is equivalent to 'has __getitem__ 
> defined', and I'd be surprised if it's an isolated usage. Python 
> actually has trouble distinguishing between sequences and mappings, as 
> anyone who as tried to use the 'isMapping' API would know (the isMapping 
> API uses much the same definition as the reference manual does - so all 
> sequences show up as mappings, as they map indices and slices to objects).
> 
> Section 3 of the reference manual is actually more use for anyone 
> developing custom types that override special methods. E.g. proper 
> handling of slice objects is described here under container emulation:
> http://www.python.org/dev/doc/devel/ref/sequence-types.html

I understand that the distinction between sequences and mappings is 
fuzzy, as both use __getitem__() for access. But the usage of 'mapping' 
in 5.3.3 is inconsistent with the section you refer to. That page says 
"It is also recommended that mappings provide the methods keys(), 
values(), items(), has_key(), get(), clear(), setdefault(), iterkeys(), 
itervalues(), iteritems(), pop(), popitem(), copy(), and update() 
behaving similar to those for Python's standard dictionary objects."

and under __getitem__(): "For *sequence* types, the accepted keys should 
be integers and slice objects."

I just think 5.3.3 is unnecessarily opaque. Particularly since the only 
built-in mapping (dict) doesn't even accept simple slices as indices.

Kent

> 
> Regards,
> Nick.
> 



More information about the Python-list mailing list