Re: Using '__mul__' within a class

2005-09-25 Thread John J. Lee
"Gerard Flanagan" <[EMAIL PROTECTED]> writes: [...] > class FibonacciMatrix: [...] > def Copy( self ): [...] __copy__ would be a more standard name. Then: import copy fm = FibonacciMatrix() fm2 = copy.copy(fm) I suppose you could also add: __deepcopy__ = __copy__ in the body of the clas

Re: Using '__mul__' within a class

2005-09-24 Thread Gerard Flanagan
Thanks for all the replies - you are very nice people! Don't worry Jeff, I assumed you weren't telling me what 1*1 equals! I got your point. James Stroud impressively got to the heart of what I was trying to do - which was just to wrap up the code here: en.wikipedia.org/wiki/Fibonacci_number_pr

Re: Using '__mul__' within a class

2005-09-24 Thread Piet van Oostrum
> James Stroud <[EMAIL PROTECTED]> (JS) wrote: >JS> def Multiply(self, other): >JS> self.a = self.a * other.a + self.b * other.b >JS> self.b = self.a * other.b + self.b * other.c >JS> self.c = self.b * other.b + self.c * other.c I gues this will give the wrong

Re: Using '__mul__' within a class

2005-09-24 Thread jepler
Oops! I should have used '2' in the example, or some other number. I was trying to make a point about what x *= ... means when it is in the context of a function (or method) body. I think this is key to understanding what Python did in the case the user posted. Being rude wasn't my inte

Re: Using '__mul__' within a class

2005-09-24 Thread James Stroud
On Saturday 24 September 2005 09:07, [EMAIL PROTECTED] wrote: > For the same reason that > def f(z): > z *= z > c = 1 > f(c) > print c > prints 1. > > Jeff I don't mean to be rude, but this is a horrible example if your are intending to help a neophyte: py>

Re: Using '__mul__' within a class

2005-09-24 Thread James Stroud
Shoot, Square() should be: def Square(self): self.Multiply(self) Forgot to proofread before hitting send. James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ -- http://mail.python.org/mailman/listinfo

Re: Using '__mul__' within a class

2005-09-24 Thread Martin Miller
As others have pointed out, you are just reassigning a new value to the self argument in the Square() method shown. Instead, what you need to do is change the object that 'self' refers to within the method. To do this, change it to: def Square( self ): result = self * self self

Re: Using '__mul__' within a class

2005-09-24 Thread James Stroud
Additionally, your __mul__() returns a new FibonnacciMatrix. You do not want a new FibbonacciMatrix, you want to operate on an existing matrix (otherwise, you would want to go with Ivan Voras's solution, where you re-assign outside of the class). If you don't want the overhead of creating a inst

Re: Using '__mul__' within a class

2005-09-24 Thread James Stroud
I think the gist of your problem is that you are re-binding self in the method. Here is a simpler example of your problem: py> def doit(c): ... c = 5 ... print "c in the method is", c ... py> c = 42 py> print "c before calling the method is", c c before calling the method is 42 py> doit(c) c

Re: Using '__mul__' within a class

2005-09-24 Thread Terry Reedy
"Gerard Flanagan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >def __mul__( self, other ): >def Square( self ): >self *= self Among the other reasons cited, I believe that 'op=' maps to a different special method than 'op'. __imul__? or something? do check. tj

Re: Using '__mul__' within a class

2005-09-24 Thread Ivan Voras
Gerard Flanagan wrote: > def Square( self ): > self *= self You probably mean return self * self > A = FibonacciMatrix() > A.Square() Make this A = A.Square() -- http://mail.python.org/mailman/listinfo/python-list

Re: Using '__mul__' within a class

2005-09-24 Thread jepler
For the same reason that def f(z): z *= z c = 1 f(c) print c prints 1. Jeff pgpF5jQBO3otJ.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list

Using '__mul__' within a class

2005-09-24 Thread 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