[Python-Dev] Re: d = {}; d[0:1] = 1; d[0:1] = 2; print d[:]

Donn Cave donn at oz.net
Fri Mar 2 00:55:58 EST 2001


Quoth Thomas Wouters <thomas at xs4all.net>:
|
| [ Dicts accept slice objects as keys in assignment, but not in retrieval ]
|
...
| I think making slices hashable objects makes the most sense. They can just
| be treated as a three-tuple of the values in the slice, or some such.
| Falling back to just sq_item/__getitem__ and not mp_subscript might make
| some sense, but it seems a bit of an artificial split, since classes that
| pretend to be mappings would be treated differently than types that pretend
| to be mappings.

The problem for me is that when we write ob[i:j], we clearly
must have expected that ob is a sliceable sequence.  If it is
instead a mapping, then the expression has no sensible meaning,
so it's an error and ought to raise an exception.

I'm not all that devastated to hear that my ceval.c hack wasn't
the right thing, it was a pretty hasty experiment.  But I do
think that ob[i:j] should raise an exception for dict objects.
I don't care so much if ob[i:j:1] does, but I would if that
notation started working for lists and tuples.

I think it's OK to apply this logic to mapping types, and defer
it for mapping classes.  It's up to the class implementor to
decide whether a slice is meaningful there.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list