Allowing zero-dimensional subscripts

Fredrik Lundh fredrik at pythonware.com
Fri Jun 9 06:32:00 EDT 2006


Sybren Stuvel wrote:

> Just curious: how would you initialize 'x' in such a case? If I simply
> say 'x = []' and then try to index it with x[1, 2], I get "TypeError:
> list indices must be integers".

that's up to the x implementation to decide, of course:

 >>> class MyContainer:
...     def __getitem__(self, index):
...         return index
...
 >>> x = MyContainer()
 >>> x[1]
1
 >>> x[1, 2]
(1, 2)
 >>> x[(1, 2, 3)]
(1, 2, 3)
 >>> x[()]
()

noam's proposal is to make this work:

 >>> x[]
()

(but should it really result in an empty tuple?  wouldn't None be a bit 
more Pythonic?)

</F>




More information about the Python-list mailing list