A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/311896.html below:

Using '__mul__' within a class

Using '__mul__' within a class Using '__mul__' within a classGerard Flanagan grflanagan at yahoo.co.uk
Sat Sep 24 11:36:04 EDT 2005
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'

------------------------------


More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4