
On Sun, Jul 30, 2000 at 12:29:58PM -0500, Guido van Rossum wrote:
So the most preferred way is to pass a single argument which is either:
- A single object (directly what was passed in) for a single index. - A slice-object if any kind of single-slice was passed in, which holds the zero-to-three objects that the slice was 'created' with. - A tuple of zero or more of either of the above two.
(Make that a tuple of two or more!) Indexing like a[] is illegal, and indexing like a[i] passes a non-tuple.
No sir:
class X: ... def __getitem__(self, val): ... return val ... x = X() x[()] () x[1,] (1,)
While I'm writing this down, I'm not sure if it's such a good idea. Isn't this placing a tad too much into one function ? It might require some severe logic to support this all, especially if you give 'special' meanings to some indexes. And we're inserting a new catch-all method -- a set of them, actually: get, set and del -- and that while Paul is trying to solve the other catch-all Python has, __getattr__/__setattr__.
Actually, __getitem__ *already* has to deal with -- the only case it doesn't get currently is the step-less single slice, which gets passed to __getslice__ -- and dies if it's not there.
It should, but it doesn't, in most cases. God knows the code I wrote that had getitem and getslice didn't take slice objects into account, mostly because I didn't know how they worked ! Not to mention slice-tuples, which I didn't know were possible until you mentioned it ;)
And lets not forget the convenience of writing those methods: __getitem__ is intuitive, both in name and semantics. So is __getslice__. The passing of a slice object to __getitem__ is a tad less intuitive, and a tuple of index/slice objects even less.
Don't worry, that's already the case.
I know, I was arguing for an improvement, here :)
I'm tempted to suggest a single change: when __getslice__ is not defined, pass a slice object (with no step, as if the slice had a trailing ':') to __getitem__, and document it properly.
Great! That's *exactly* what I've been proposing.
[snip more approvals]
I suspect this is all for 2.1 or later, though.
Actually, the actual change (fall back on __*item__ with a slice object when __*slice__ doesn't exist) should be simple to add for 2.0. Just submit to the SF PM!
That's what I thought. I'll see about that inbetween augmented assignment (which is not easy to do right, in retrospect. I almost uploaded a patch that evaluated 'x' in 'x += 1' twice ;) unless someone beats me to it, of course. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!