I'm teaching myself Python via an online wikibook, and came across a confusing error in one of the examples with overloading operators. According to the example:
class FakeNumber:
n = 5
def __add__(A,B):
return A.n + B.n
c = FakeNumber()
d = FakeNumber()
d.n = 7
c.__imul__ = lambda B: B.n - 6
c *= d
c
is supposed to return:
1
but instead I get:
TypeError: unsupported operand type(s) for *=: 'FakeNumber' and 'FakeNumber'
I get that you can't multiply objects together, so then what is the point of c.__imul__ = lambda B: B.n - 6
? Is there something missing, or where is there improper syntax?
Reference: http://en.wikibooks.org/wiki/Python_Programming/Classes#Operator_Overloading
Indeed the code is working as intended in python 2, but not in 3. A possible fix in python 3 would be the following:
class FakeNumber:
def __init__(self, i):
self.i = i
def __imul__(self, B):
self.i = self.i * B.i
return self
a = FakeNumber(5)
b = FakeNumber(6)
a *= b