operator overloading

looping kadeko at gmail.com
Wed Apr 4 05:36:52 EDT 2007


Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.

Here is my try:
>>> class myint(int):
	def __pow__(self, value):
		return self.__add__(value)

>>> a = myint(3)
>>> a ** 3
6

OK, it works. Now I try different way to achieve the same result but
without much luck:

>>> class myint(int):
	pass
>>> myint.__pow__ = myint.__add__

or:
>>> class myint(int):
	__pow__ = int.__add__

or:
>>> class myint(int):
	pass
>>> a.__pow__ = a.__add__

but for every try the result was the same:
>>> a = myint(3)
>>> a ** 3
27

Why it doesn't works ?




More information about the Python-list mailing list