How to keep a function as a generator function when the yield operator is moved into its sub-functions??

Diez B. Roggisch deets at nospam.web.de
Tue Jul 14 22:59:04 CEST 2009


weafon schrieb:
> Hi guys,
> 
> I have a question about the usage of yield. As shown in the below 
> example, in general, if there is a code segment commonly used by two or 
> more functions, we may isolate the segment into a function and then call 
> it from other functions if necessary.
> 
> def func1():
>    ....
>    while(cond):
>        .....
>        commoncode()
>        ...
> 
> 
> def func2():
>    ....
>    while(cond):
>        .....
>        commoncode()
>        ...
> 
> def commoncode()
>    AAAA
>    BBBB
>    CCCC
> 
> However, if there is a 'yield' operation in the common code segment, the 
> isolation causes that func1 and func2 become a non-generator function!! 


No. Not writing them as generators makes them a non-generator-function.

You are way to unspecific with your examples. But if func1 and func2 are 
themselves supposed to be generators, there is nothing preventing you 
from using them as such.

Diez



More information about the Python-list mailing list