range(Instance) vs. foo[Instance]

Jeff Epler jepler at inetnebr.com
Tue Jul 6 10:13:36 EDT 1999


Suppose I have a class with __int__ defined:

class c:
	def __init__(self, val):
		self.val = int(val)

	def __int__(self): return self.val

>>> x=c(1)
>>> range(x)
[0]
>>> (0,1,2)[x]
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: sequence index must be integer

It seems desirable that the subscript operator call x.__int__ just like
range() must effectively do.  Is there some reason why not?

(Not the subscript operator in general, but the subscript operator for
the builtin types of lists and tuples)

I ask because I recently wrote a 5-minute hack on top of bytecodehacks that
makes "integers" function as rational numbers in the interactive Python
console---functions like "range" work, but subscripting of tuples and lists
is broken.

Jeff




More information about the Python-list mailing list