On 27 June 2013 03:41, Andrew Barnert <abarnert@yahoo.com> wrote:
On Jun 26, 2013, at 16:25, Joshua Landau <joshua.landau.ws@gmail.com> wrote:
On 27 June 2013 00:17, Andrew Barnert <abarnert@yahoo.com> wrote:
In case it's not obvious how ridiculous it is to extrapolate MISRA-C to other languages…
I went through the 127 rules in MISRA-C 1998. About 52 of them could be extrapolated to Python. Of those, 20 make some sense; the other 32 would be either ridiculous or disastrous to apply in Python. (In fact, much the same is true even for more closely-related languages like C++ or JavaScript.)
If you're curious about the details, see http://stupidpythonideas.blogspot.com/2013/06/misra-c-and-python.html.
"67. rcm: Don't rebind the iterator in a for loop."
That sounds quite sane to me...
Why?
In C, the loop iterator is a mutable variable; assigning to it changes the loop flow.
for (i=0; i!=10; ++i) { if (i % 2) { i = 0; } printf("%d\n", i); }
That looks like it should alternate even numbers with 0's, up to 10. But it actually produces an infinite loop of 0's. Very confusing.
In Python, the loop iterator is just a name for a value; assigning to it does not change that value or affect the flow in any way; it just gives you a convenient name for a different value.
for i in range(10): if i % 2: i = 0 print i
That looks like it should alternate even numbers with 0's, up to 10. And that's exactly what it does.
Obviously this is a silly toy example, but there's plenty of real code that does this kind of thing.
My response applies to Ethan as well; it'll be pointless to post it twice. You are not rebinding the *iterator* but the, um, iteree? Rebinding the iterator, to me, would look more like:
looper = range(10) for value in looper: ... looper = range(value-2) ... print(value, end="") ... 0123456789
Which is just a confusing thing to do, or (even worse):
looper = list(range(10)) for value in looper: ... looper.append(value) ... print(value, end="") ... 01234567890123456789012345678901234567890123456...
Which can be very bad if you do it on hashed things (when you could), and is really quite confusing. There are so very few reasons to do this that "just avoid it" is a good response, á mon avis.