Ned, I still think you're missing the point of my original post. A return is as good as a break when considering early exit from a loop. The pylint code only checks for the presence of a break statement, but the test cases clearly show a case with a return statement. The test functions even include docstrings which state that. That's all I'm trying to point out. That I discovered the problem in a piece of code which could be rewritten to avoid the warning is quite beside the point. Skip
On 18 September 2013 02:39, Skip Montanaro <skip@pobox.com> wrote:
Ned,
I still think you're missing the point of my original post. A return is as good as a break when considering early exit from a loop. The pylint code only checks for the presence of a break statement, but the test cases clearly show a case with a return statement. The test functions even include docstrings which state that.
It's not considering the exit from the loop it's considering the entry to the else clause. A break would continue past the body of the loop without entering the else clause (so the else would have a meaning - only enter this code if we haven't hit break). Without a break the else clause will always be entered if the loop terminates normally, so the else is useless. Just putting the return immediately after the loop is functionally identical. That you have some early returns inside the body of the loop is irrelevant - the "else" is still unnecessary / pointless. Michael
That's all I'm trying to point out. That I discovered the problem in a piece of code which could be rewritten to avoid the warning is quite beside the point.
Skip _______________________________________________ code-quality mailing list code-quality@python.org https://mail.python.org/mailman/listinfo/code-quality
-- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html
On 18 septembre 05:33, Michael Foord wrote:
On 18 September 2013 02:39, Skip Montanaro <skip@pobox.com> wrote:
I still think you're missing the point of my original post. A return is as good as a break when considering early exit from a loop. The pylint code only checks for the presence of a break statement, but the test cases clearly show a case with a return statement. The test functions even include docstrings which state that.
It's not considering the exit from the loop it's considering the entry to the else clause. A break would continue past the body of the loop without entering the else clause (so the else would have a meaning - only enter this code if we haven't hit break). Without a break the else clause will always be entered if the loop terminates normally, so the else is useless. Just putting the return immediately after the loop is functionally identical.
That you have some early returns inside the body of the loop is irrelevant
Yep, that's the point of this check so IMO Pylint doesn't have to be changed. May Torsten or someone else from Google which has contributed this check may confirm this is the expected behaviour. Skip, regarding the test you found: def test_return_for(): """else + return is accetable.""" for i in range(10): if i % 2: return i else: print 'math is broken' the docstring doomed you: the else is actually warned here (see associated message file in test/messages/func_useless_else_on_loop.txt). I'll fix that. -- Sylvain Thénault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42) Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations Développement logiciel sur mesure: http://www.logilab.fr/services CubicWeb, the semantic web framework: http://www.cubicweb.org
participants (3)
-
Michael Foord
-
Skip Montanaro
-
Sylvain Thénault