Assignment is bad
This is basically the functional language position.
And much of Backus landmark Turing award
remains relevant nearly 50 years on.
We could rehash those arguments if we wish; but it's good to have something like the Backus TAL as a point of departure for that.
Mutation
Mutation often looks like assignment but is worse
A small Python session showing
- no assignment
- assignment,
- mutating function
- mutating assignment
Session
# Functional behavior; no assignment/mutation no problems
>>> a = [1,2,3]
>>> c = [a,a]
>>> c
[[1, 2, 3], [1, 2, 3]]
>>> a+[4]
[1, 2, 3, 4]
>>> c
[[1, 2, 3], [1, 2, 3]] # unchanged
# Assignment; but still harmless
>>> a =[1,2,3]
>>> c = [a,a]
>>> c
[[1, 2, 3], [1, 2, 3]]
>>> a = a + [4]
>>> a
[1, 2, 3, 4] # changed as expected
>>> c
[[1, 2, 3], [1, 2, 3]] #unchanged as expected
>>>
#append looks like + but it actually mutates
>>> a = [1,2,3]
>>> c = [a,a]
>>> c
[[1, 2, 3], [1, 2, 3]]
>>> c[0].append(4)
>>> c
[[1, 2, 3, 4], [1, 2, 3, 4]]
# Note how changing c[0] changed c[1]
# ie the unholy alliance between aliasing&mutation
>>> d = [[1,2,3] for i in [1,2]]
>>> d
# A d which looks just like c
[[1, 2, 3], [1, 2, 3]]
>>> d[0].append(4)
>>> d
[[1, 2, 3, 4], [1, 2, 3]]
# But changing c[0] doesn't change c[1]
# ie different aliasing
>>> a = [1,2,3]
>>> c = [a,a]
>>> a[:] = [4,5] # mutation note the ':'
>>> c
[[4, 5], [4, 5]] # WHAZZAT!
What do I recommend
1st choice
Use a language like Haskell -- no assignment, no mutation.
Or at least Scheme/ML where these are mostly not used
2nd choice
Use a language like Python as a "functional assembly language".
ie Use it like a functional language; don't teach mention append/extend etc the mutating methods.
When some smart Alec comes up using these break his code in the above paradigm!
This is what I often end up doing.
3rd choice
Teach all the mess and confuse the students