Possible to use more than one argument on __getitem__?

__getitem__ only accepts one argument (other than self), so you get passed a tuple.

You can do this:

class matrix:
    def __getitem__(self, pos):
        x,y = pos
        return "fetching %s, %s" % (x, y)

m = matrix()
print m[1,2]

outputs

fetching 1, 2

See the documentation for object.__getitem__ for more information.

Leave a Comment