Extention to the integer class.

Gregoire Welraeds greg at perceval.be
Mon Mar 13 08:59:29 EST 2000


Hello pythoniens!

I got a question for you... 

I want to extend the integer class to add two stupid methods :
i.i(x)	# inc i with x or with 1 if x is not present 
i.d(x)	# dec i of x or of 1 if x is not present

So I have done the following class :
class Int:
	def __init__(self,value):
		self.__val= value
	def __cmp__(self, value):
		if self.__val < value:	
			return -1
		elif self.__val == value:
			return 0
		else: return 1
	def i(self, value= 1):
		self.__val= self.__val + value
		return self.__val
	def d(self, value= 1):
		self.__val= self.__val - value
		return self.__val

Now this work with :
i = i+a 	=> i.i(a)
i = i-a		=> i.d(a)
i++		=> i.i()
i--		=> i.d()
Thanks to the __cmp__() methods, it also work with :
i < a
i >= a
i <= a
i > a
...

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]

So my question is double :
	can I extend the Integer class to add inc and dec ?
	Is there a way to write i and have the interpreter understand
	i.get() or something similar (as it may do with plain integer) ?

I understand that the interest of the goal is somewhat limited... but
thanks :))


--
Life is not fair
But the root password helps
--

Gregoire Welraeds
greg at perceval.be
Perceval Development team
-------------------------------------------------------------------------------
Perceval Technologies sa/nv	Tel: +32-2-6409194		
Rue Tenbosch, 9			Fax: +32-2-6403154		
B-1000 Brussels			general information:   info at perceval.net
BELGIUM				technical information: helpdesk at perceval.net
URL: http://www.perceval.be/
-------------------------------------------------------------------------------





More information about the Python-list mailing list