[Python-projects] yield != return

Maarten ter Huurne maarten at treewalker.org
Fri Sep 12 10:51:26 CEST 2008


On Friday 12 September 2008, Sylvain Thénault wrote:
> On Tue, Sep 09, 2008 at 09:55:10AM -0500, skip at pobox.com wrote:
> > 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.
>
> True. I've added a ticket: http://www.logilab.oorg/ticket/5977
>
> One possibility is to count subsequent yield as one. Another to don't
> have this kind of warning for yield. Opinion ?

Yield is different from return since it does not end the control flow 
through the method. If you want to know what is returned by a method, you 
have to check the return statements, which is easier if there aren't too 
many of them. If you want to know what is yielded by a method, you have to 
read the entire control flow anyway, since one yield statement can be 
activated more than once. So I think yield statements should not be counted 
as return statements in any way.

It might be useful to have a separate rule + limit for the number of yield 
statements in a function. However, I think that methods that have too many 
yield statements are too complex not because of those yield statements, but 
for other reasons (too long, too many branches etc). In other words: a 
too-complex function might contain many yield statements. So in my opinion, 
a warning for too many yields is not needed, but if it is implemented, 
please make it separate from the warning about returns.

Bye,
		Maarten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part.
Url : http://lists.logilab.org/pipermail/python-projects/attachments/20080912/4b5ce00b/attachment.pgp 


More information about the Python-Projects mailing list