W
weafon
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!!
Although I can prevent such an isolation by just duplicating the segment
in func1 and func2 to keep both of them being generator functions, the
code may become ugly and hard to maintain particularly when coomoncode()
is long.
The problem may be resolved if I can define the commoncode() as an
inline function or marco. Unfortunately, inline and marco do not seems
to be implemented in python. Thus, how can I isolate a common segment
into a function when there are yield operations in the common segment?
Thanks,
Weafon
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!!
Although I can prevent such an isolation by just duplicating the segment
in func1 and func2 to keep both of them being generator functions, the
code may become ugly and hard to maintain particularly when coomoncode()
is long.
The problem may be resolved if I can define the commoncode() as an
inline function or marco. Unfortunately, inline and marco do not seems
to be implemented in python. Thus, how can I isolate a common segment
into a function when there are yield operations in the common segment?
Thanks,
Weafon