[Python-projects] yield != return

skip at pobox.com skip at pobox.com
Tue Sep 9 16:55:10 CEST 2008


Pylint seems to treat yield statements as if they were returns.  They
definitely seem to be counted the same and the error message emitted with
too many yields even calls them "returns":

    yieldex.py:1: [R, yield_ex] Too many return statements (10/6)

Here's a trivial example cobbled together from an actual (slightly more
complex) functio which generates a Python function from a higher level
input:

    def yield_ex(s):
        if s:
            yield "    line 1: %s\n" % s
            yield "    line 2\n"
            yield "    line 3\n"
            yield "    line 4\n"
            yield "    line 5\n"
        else:
            yield "    line 6\n"
            yield "    line 7\n"
            yield "    line 8\n"
            yield "    line 9\n"
            yield "    line 10\n"

I found it convenient to yield each line of the generated function.  The
code is hardly more complex than the above though there are some parameter
substitutions on several lines.  Having just two yield statements would have
made it less obvious which values were being substituted where.  (Though I
will admit I could have used dict format expansion.)

At any rate, I don't think yield statements should be counted the same way
returns are.

-- 
Skip Montanaro - skip at pobox.com - http://www.webfast.com/~skip/


More information about the Python-Projects mailing list