How to Think Like A Computer Scientist
Ironically, apropos of the recent discussion How to Think Like A Computer Scientist - Java Version introduces the fundamental concepts of Object Oriented Programming by way of the creation of a mutable complex number. http://www.ibiblio.org/obp/thinkCSjav/chap13.htm FWIW. Art
Are you sure? public static Complex add (Complex a, Complex b) { return new Complex (a.real + b.real, a.imag + b.imag); } To invoke this method, we would pass both operands as arguments: Complex sum = add (x, y); Written as an object method, it would take only one argument, which it would add to the current object: public Complex add (Complex b) { return new Complex (real + b.real, imag + b.imag); } Looks immutable to me, i.e. we're not changing in place but returning a new complex. I guess you're referring to the fact that real and imag aren't private e.g.: Complex x = new Complex (); x.real = 1.0; x.imag = 2.0; Complex y = new Complex (3.0, 4.0); Yes, that's a change-in-place ability, true. Kirby On 3/28/06, Arthur <ajsiegel@optonline.net> wrote:
Ironically, apropos of the recent discussion
How to Think Like A Computer Scientist - Java Version introduces the fundamental concepts of Object Oriented Programming by way of the creation of a mutable complex number.
http://www.ibiblio.org/obp/thinkCSjav/chap13.htm
FWIW.
Art
_______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
-----Original Message----- From: Arthur [mailto:ajsiegel@optonline.net] Sent: Tuesday, March 28, 2006 9:12 AM To: 'kirby urner'; 'Arthur' Cc: edu-sig@python.org Subject: RE: [Edu-sig] How to Think Like A Computer Scientist
From: kirby urner [mailto:kirby.urner@gmail.com]
Complex x = new Complex (); x.real = 1.0; x.imag = 2.0; Complex y = new Complex (3.0, 4.0);
Yes, that's a change-in-place ability, true.
Kirby
And exactly what I need to have my Complexes do the PyGeo dance.
The developer of this math library for Eiffel includes both mutable and immutable complex number classes. http://www.eiffelzone.com/esd/mathw/index.html To what extent that reflects the approach taken in the book he references: Object-Oriented Implementation of Numerical Methods; ISBN: 1-55860-679-3. can't say. Art
So we have different levels of mutability. A complex type which doesn't allow resassignment of real and imaginary parts might look like: Immutable: c = Complex(1,2) c = c.real(3) # assigning a new value c = c.imag(4) # yet another new value Mutable: c = Complex(1,2) c.real = 3 c.imag = 4 Even more mutable: c + c # changes c to 2c c**2 # 2nd power of c (changes c "in place") All are codable in Python of course. Of these three possibilities, my own bias is in favor of the least mutable (top example). Arthur is proposing first level mutability (2nd example). I doubt any of us like the 3rd level. Am I right? Note that Python's built-in complex number doesn't even allow the top example. Instead, one might go: Even more immutable: c = Complex(1,2) c = Complex(3, c.imag) # assigning a new value c = Complex(c.real, 4) # yet another new value So obviously there's a spectrum here. Python supports them all, with coding. Kirby
On Tuesday 28 March 2006 09:42, kirby urner wrote:
So we have different levels of mutability. A complex type which doesn't allow resassignment of real and imaginary parts might look like:
Immutable:
c = Complex(1,2) c = c.real(3) # assigning a new value c = c.imag(4) # yet another new value
Actually is a mutable version. Allowing the change to the components after creation makes it mutable. It does not matter if the change is through mutating methods or by direct attribute access. The point is that the object itself changes. If two variables reference the same object (or two threads manipulate it) they will both see the changes. An immutable complex would simply disallow changes. To "change" either component would require creation of a new object.
Mutable:
c = Complex(1,2) c.real = 3 c.imag = 4
Even more mutable:
c + c # changes c to 2c c**2 # 2nd power of c (changes c "in place")
All are codable in Python of course. Of these three possibilities, my own bias is in favor of the least mutable (top example). Arthur is proposing first level mutability (2nd example). I doubt any of us like the 3rd level. Am I right?
Note that Python's built-in complex number doesn't even allow the top example. Instead, one might go:
Even more immutable:
c = Complex(1,2) c = Complex(3, c.imag) # assigning a new value c = Complex(c.real, 4) # yet another new value
So obviously there's a spectrum here. Python supports them all, with coding.
Kirby _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
-- John M. Zelle, Ph.D. Wartburg College Professor of Computer Science Waverly, IA john.zelle@wartburg.edu (319) 352-8360
On 3/28/06, John Zelle <john.zelle@wartburg.edu> wrote:
On Tuesday 28 March 2006 09:42, kirby urner wrote:
So we have different levels of mutability. A complex type which doesn't allow resassignment of real and imaginary parts might look like:
Immutable:
c = Complex(1,2) c = c.real(3) # assigning a new value c = c.imag(4) # yet another new value
Actually is a mutable version. Allowing the change to the components after creation makes it mutable. It does not matter if the change is through mutating methods or by direct attribute access. The point is that the object itself changes. If two variables reference the same object (or two threads manipulate it) they will both see the changes.
An immutable complex would simply disallow changes. To "change" either component would require creation of a new object.
But the above doesn't allow any change to a complex number. Rather, c.imag(3) outputs a new complex number, leaving c unchanged. Except I immediately reassign the new value to c. Something like: class Complex(object): def __init__(self, v, z): self.r = v self.i = z def imag(v): return Complex(self.r, v) def real(v): return Complex(v, self.i) # plus code to make i, r read-only. Just plain c.imag(3) would not change c, plus the returned value would be lost, given there's no assignment. Sorry if my example was unclear. Kirby
----- Original Message ----- From: kirby urner <kirby.urner@gmail.com>
Arthur is proposing first level mutability (2nd example). I doubt any of us like the 3rd level. Am I right?
I am not proposing anything. I am programming.
So obviously there's a spectrum here. Python supports them all, with coding.
More generally, Python supports programmer freedomn, and accepts the cost of that freedom. If we don't exercise that freedom, Python - it seems to me - is a losing proposition. Art
More generally, Python supports programmer freedomn, and accepts the cost of that freedom. If we don't exercise that freedom, Python - it seems to me - is a losing proposition.
Art
Programmer freedom means more opportunities to shoot oneself in the foot with bad designs. Exercising freedom doesn't have to mean suspending one's judgment as to what's a pitfall. Kirby
----- Original Message ----- From: kirby urner <kirby.urner@gmail.com>
More generally, Python supports programmer freedomn, and accepts the cost of that freedom. If we don't exercise that freedom, Python - it seems to me - is a losing proposition.
Art
Programmer freedom means more opportunities to shoot oneself in the foot with bad designs.
Exercising freedom doesn't have to mean suspending one's judgment as to what's a pitfall.
Hmmm? Didn't think of that. Thank you Mr. Urner. How my doing, Dethe? Art
----- Original Message ----- From: kirby urner <kirby.urner@gmail.com>
Programmer freedom means more opportunities to shoot oneself in the foot with bad designs.
You certainly don't need to confront that issue in the typical Urner 10 line script. Though I am not sure I can imagine something worse than your @property laden triangle in so many lines. You might have more sympathy for what I am confronted with if you went a little further. Its that damn 11th line that's the killer. Sorry Dethe. Art
participants (4)
-
ajsiegel@optonline.net
-
Arthur
-
John Zelle
-
kirby urner