Extention to the integer class.
Anthony J Wilkinson
anthony at dstc.edu.au
Mon Mar 13 10:47:01 EST 2000
Gregoire Welraeds wrote:
> class Int:
[...]
> But It fails when i use i as an index in a list ex: list[i]
> I could create a get method :
> def get(self):
> return self.__val
> but it is very poor since each time i is a index, i have to write i.get().
> ex: list[i.get]
You could also create a __int__ method:
def __int__(self):
return self.__val
which would be used as: list[int(i)]
Alternatively you could use a __call__ method:
def __call__(self):
return self.__val
which would be used as: list[i()]
Java has similar problems with a mix of 'primitive' types (ints, chars,
doubles, etc) and instance types. It makes a lot of java very ugly - I
recently had to write:
cost = Integer.valueOf(costString.trim()).intValue();
which is equivalent to the python:
cost = int(costString)
So it seems to me that python handles these things a little better than
Java does.
Anthony
More information about the Python-list
mailing list