Improving Wetware

Because technology is never the issue

Introductory Programming Texts Need Work

Posted by Pete McBreen Sat, 11 Dec 2010 05:55:00 GMT

Had a need to look at Python textbook recently and got confused several times by the examples that were needlessly obfuscated (slightly paraphrased code):

>>> def actions():
...     acts = []
...     for i in range(5):
...         acts.append(lambda x, i=i: i **x)
...     return acts
>>> acts = actions() 
>>> acts[3](2)
  1. For a newbie, it would not be obvious that acts inside the function is NOT the same variable as the same variable acts outside of the function.
  2. The i=i is designed g=to be confused, the first one is a new name in the scope of the lambda, the second is from the for loop. The first should have had a different name to make it obvious what was happening.
  3. The name of the function actions just does not communicate anything.

I’m going to have to think on this more, but maybe my next project is going to involve creating some material for newbies.