[Edu-sig] teaching Python

Mark Engelberg mark.engelberg at alumni.rice.edu
Fri Apr 23 19:44:00 CEST 2010


They have every right to be confused.  Who would guess that Python
evaluates optional arguments once at compile-time?  Heck, I've used
Python for years and I wouldn't have been able to tell you off the top
of my head how Python handles this code.

That reminds me of a similar gotcha I've unfortunately run into on
more than one occasion.

# intialize a to be a list of 5 empty lists
a = 5*[[]]
# push a value onto the first list.
a[0].append(1)

What's a?

--Mark

(P.S.  Stuff like this drives me nuts, which is why I find myself
gravitating more and more to languages with a richer set of immutable
data structures, e.g., Clojure/Scala/etc., where both of these gotchas
would become a non-issue.  I would love to see more immutable
datatypes in Python other than numbers/strings/tuples, although
realistically, I'd say that's unlikely to happen.)


More information about the Edu-sig mailing list