def move(p, U):
q = []
for i in range(len(p)):
s = pExact * p[(i-U) % len(p)]
s = s + pOvershoot * p[(i-U-1) % len(p)]
s = s + pUndershoot * p[(i-U+1) % len(p)]
q.append(s)
return q
where the index is used to access the previous and next elements in a list.There's a more idiomatic way to get this index variable(http://stackoverflow.com/questions/522563/accessing-the-inde...), but using a consistent, easy to understand iteration method allows the poor confused student (i.e. me) to spend more time focusing on the concept being taught.
So the code could be intentional, or could be an accident. But I expect that sooner rather than later there will be some data to show which method, if any, is better for the students, and the classes will be adjusted accordingly.
By the way, did anyone else find these exercises addictive? I can begin to appreciate why the Farmville cow-clickers keep at it.
I am aware of two reasonable answers to the "how do I learn idiomatic style" question (in any language, although Python is the only language in which I personally have deliberately sought a better accent).
1) Read great codebases. For Python, the #1 target here is probably the standard library. People also often mention high-profile projects like Django or Twisted; since I haven't tried that I don't know what percentage of that code is beautiful, but I'm guessing it's probably a mix (so you'd have to apply some judgement).
2) The path I have taken is to read a lot of blogs and Stack Overflow questions and so on. Consider this: http://stackoverflow.com/search?q=pythonic In particular, answers by Alex Martelli are reliably very informative (even if I find his prose style a little sub-optimal).
3) Oh, and this is a bit abstract, but if you haven't typed 'import this' into your Python interpreter, you should do that, for some guidance about the abstract ethics of "being Pythonic".
I agree that simplicity of learning is a good goal. This is why I didn't raise the issue of the several dozen times where he uses an very ugly for loop where a very nice list comprehension would be the Pythonic thing to do. Being Pythonic is not the highest goal in software, I get that, and if you're trying to focus on learning robotics rather than learning Python, list comprehensions add friction.
But in the case of iteration across a list, using an idiomatic iterator is far from unique to python, and it's very readable to someone who has never seen it.
And I thought about the C/C++ thing, but the reason I said "Pascal programmer" rather than "C programmer" is because surely no-one trying to ease the translation to C would have used all those list.append() calls with which Sebastian is so liberal.
# less readable, unpythonic, but a single expression
reduce(sense, measurements)