Some things to remember when implementing __getitem__

February 24, 2007

I've recently been doing some work on classes derived from list and tuple that fiddle with the behavior of __getitem__, and ran into a couple of surprises that I am going to write down so that I remember them in the future:

  • for the simple 'obj[i:j]' case, __getitem__ is not called if the class has a __getslice__ method. list and tuple both do, despite the method being labeled as deprecated since Python 2.0.

  • the moment you use an i or j that is not a simple integer (including floating point numbers, but not including long integers), it turns into a call to __getitem__ with a slice object as the key. (This is simple slicing.)

Supporting the full slice syntax in a __getitem__ implementation makes my head hurt; you can get handed a slice object, or a tuple that can contain at least slice objects, ellipsis objects, and numbers (and probably more that I don't know about). Just throw TypeError instead; it's what lists and tuples do.

Checking that your __getitem__ has not been called with such things, so that you can throw TypeError appropriately, is harder than it should be. I personally wish that __getitem__ wasn't so generic; it seems un-Pythonic to have to inspect the type of your argument to figure out what to do with it.

(The better way would be to have one method to implement plain subscripting, one for simple slices, and a third one for extended slicing. Unfortunately it's too late for that now.)

Written on 24 February 2007.
« The downside of distinctive hostnames
Most world-editable wikis are doomed »

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sat Feb 24 01:07:40 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.