G
Gerard Flanagan
Hello
I'm pretty new to Python and was wondering why the 'Square' method in
the following code doesn't work. It doesn't fail, just doesn't do
anything ( at least, not what I'd like! ). Why doesn't 'A.a' equal 2
after squaring?
TIA.
class FibonacciMatrix:
def __init__( self ):
self.a = 1
self.b = 1
self.c = 0
def __mul__( self, other ):
result = FibonacciMatrix()
result.a = self.a * other.a + self.b * other.b
result.b = self.a * other.b + self.b * other.c
result.c = self.b * other.b + self.c * other.c
return result
def Square( self ):
self *= self
A = FibonacciMatrix()
A.Square()
print A.a #prints '1'
A = FibonacciMatrix()
B = A * A
print B.a #prints '2'
------------------------------
I'm pretty new to Python and was wondering why the 'Square' method in
the following code doesn't work. It doesn't fail, just doesn't do
anything ( at least, not what I'd like! ). Why doesn't 'A.a' equal 2
after squaring?
TIA.
class FibonacciMatrix:
def __init__( self ):
self.a = 1
self.b = 1
self.c = 0
def __mul__( self, other ):
result = FibonacciMatrix()
result.a = self.a * other.a + self.b * other.b
result.b = self.a * other.b + self.b * other.c
result.c = self.b * other.b + self.c * other.c
return result
def Square( self ):
self *= self
A = FibonacciMatrix()
A.Square()
print A.a #prints '1'
A = FibonacciMatrix()
B = A * A
print B.a #prints '2'
------------------------------