5 ms·
I'm a little curious about some of the style choices in python code. For example: # used by Sebastian for k in range(len(measurements)): p = s
by jholman 15y ago
I'm a little curious about some of the style choices in python code. For example:
# used by Sebastian
for k in range(len(measurements)):
p = sense(p, measurements[k])
vs
# shorter, saner, and more Pythonic
for m in measurements:
p = sense(p, m)
I guess the old aphorism applies: a good Pascal programmer can write Pascal in any language. (Not to criticize Sebastian's programming ability, only to wonder about the pedagogical choice.)
- zaph0d 15y agoA great computer scientist != a great programmer.
- deleted 15y ago[deleted]
- ender7 15y agoYeah, this looks like academic code to me. I've never found an academic codebase that didn't make you want to cry.
- deleted 15y ago[deleted]
- ahmadss 15y agosince i'm a target audience for this course (non-technical person aspiring to learn how to code), how would i learn proper programmer convention (ie the Python way) vs. the computer scientist convention?
- jholman 15y agoThe first observation is that there is no computer scientist convention, only a heterogeneous collection of bad habits. :) Again, unlike this post's uncle and grandparent, I'm unwilling to assume that Sebastian is a bad coder in general, but this particular code is unPythonic. Maybe that's because he's a bad coder, maybe that's because he had teaching goals that I don't fully understand. 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 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".
- bockris 15y agoI was at GoogleIO last year and code like this was shown as example code on a slide. I think I sighed audibly.
- nswanberg 15y agoI also thought that was odd, and used the latter version in the first few exercises. But later there was some code of the form: 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-index-in-python-for-loops 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.
- sks 15y agoThe version used by Sebastian targets a wider audience who are not familiar with the pythonic idioms and have just skimmed a bit of python to do the course. Using a loop over array index is the way many students iterate over an array. This should not reflect on Sebastian's coding ability.
- donald_draper 15y agoWhich makes it easier to translate into typical procedural languages like c and c++, which is not completely irrational to assume for embedded systems like robotics. And because many tricks and concepts will be array-index-based, you can just go ahead and use them right from the start.
- jholman 15y agoIn reply to both sks and donald_draper: 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.
- d0mine 15y ago# less readable, unpythonic, but a single expression reduce(sense, measurements)