# [Edu-sig] How to Think Like A Computer Scientist

John Zelle john.zelle at wartburg.edu
Tue Mar 28 17:50:21 CEST 2006

```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 at 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 at wartburg.edu          (319) 352-8360
```