sajmikins at gmail.com
Wed Jul 8 00:10:38 EDT 2009
On Jul 7, 4:04 pm, kj <no.em... at please.post> wrote:
> I'm having a hard time coming up with a reasonable way to explain
> certain things to programming novices.
> Consider the following interaction sequence:
> >>> def eggs(some_int, some_list, some_tuple):
> ... some_int += 2
> ... some_list += 
> ... some_tuple += (2,)
> >>> x = 42
> >>> y = (42,)
> >>> z = 
> >>> eggs(x, y, z)
> >>> x
> >>> y
> >>> z
> [42, 2]
You have transposed some_list and some some_tuple. I.e. you should be
calling eggs(x, z, y).
> How do I explain to rank beginners (no programming experience at
> all) why x and y remain unchanged above, but not z?
You don't. Rank beginners don't have enough background knowledge to
grok that code.
Why would you even tell the poor bastards about "+=" before they were
comfortable with (python's style of) function calls, immutable
integers, mutable lists and immutable tuples?
Let them use "x = x + y" until they have enough knowledge to
understand "augmented" assignment.
Syntax matters (I mean general order of things, not linguistic
syntax.) Making an omelette requires putting eggs in a pan and
cracking them, but not in that order.
> Or consider this one:
> >>> ham = [1, 2, 3, 4]
> >>> spam = (ham,)
> >>> spam
> ([1, 2, 3, 4],)
> >>> spam is ham
> >>> spam += 
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: 'tuple' object does not support item assignment
> >>> ham += 
> >>> spam
> ([1, 2, 3, 4, 5, 5],)
> What do you say to that?
I say, "Don't use augmented assignment with indexed tuples."
Seriously, python's augmented assignment is almost magical. I think
you're just making trouble for yourself and your students if you
introduce it too early.
I get python pretty well (if I say so myself) but I wouldn't know how
In : def foo(a_list):
...: a_list = a_list + 
In : n = 
In : foo(n)
In : n
In : def bar(a_list):
...: a_list += 
In : bar(n)
In : n
It's "Just The Way It Is". Freakin' magic, man.
> I can come up with much mumbling about pointers and stacks and
> heaps and much hand-waving about the underlying this-and-that, but
> nothing that sounds even remotely illuminating.
> Your suggestions would be much appreciated!
Frankly, I'm of the impression that it's a mistake not to start
teaching programming with /the bit/ and work your way up from there.
I'm not kidding. I wrote a (draft) article about this: "Computer
I really think the only good way to teach computers and programming is
to start with a bit, and build up from there. "Ontology recapitulates
I realize that doesn't help you teach python, but I wanted to put it
More information about the Python-list