[Python-ideas] if-statement in for-loop

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Wed Oct 5 05:20:34 EDT 2016


Ken Kundert writes:

 > Why isn't it the programmer that is writing the code the best
 > person to decide what is best?

Aside from what Paul said, there's a reason why this proposal is
unlikely to attract support from senior devs.

Python language design and style guides take the position that most
code is read far more often than it is written.  Unless there is an
overriding advantage, recognized by the senior developers, Python will
protect the reader by preferring syntax that keeps each control flow
line simple in preference to saving the writer keystrokes, and even
levels of indentation.

Eg, there's no question in my mind that

    for i in range(m):
        for j in range (n):
            for k in range (p):
                m_out(i, k) += m_in1(i, j) * m_in2(j, k)

is easier to read[1] than

    for i in range(m) for j in range (n) for k in range (p):
        m_out(i, k) += m_in1(i, j) * m_in2(j, k)

despite costing two lines and two levels of indentation.  YMMV, of
course, but I suspect most senior devs will disagree with you.


Footnotes: 
[1]  It's also less painstaking to fix the bug.



More information about the Python-ideas mailing list