Using '__mul__' within a class
- From: "Gerard Flanagan" <grflanagan@xxxxxxxxxxx>
- Date: 24 Sep 2005 08:36:04 -0700
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'
------------------------------
.
- Follow-Ups:
- Re: Using '__mul__' within a class
- From: Gerard Flanagan
- Re: Using '__mul__' within a class
- From: Martin Miller
- Re: Using '__mul__' within a class
- From: James Stroud
- Re: Using '__mul__' within a class
- From: James Stroud
- Re: Using '__mul__' within a class
- From: Terry Reedy
- Re: Using '__mul__' within a class
- From: Ivan Voras
- Re: Using '__mul__' within a class
- Prev by Date: Re: Problem subclassing (Newbie)
- Next by Date: Re: Productivity and economics at software development
- Previous by thread: Shed Skin Python-to-C++ Compiler 0.0.3 Release: some fixes, 3 MB Windows package
- Next by thread: Re: Using '__mul__' within a class
- Index(es):
Relevant Pages
|