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

kirby urner kirby.urner at gmail.com
Tue Mar 28 17:42:33 CEST 2006


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


More information about the Edu-sig mailing list