这将在紧急情况下起作用:def cfor(start, test_func, cycle_func):
"""A generator function that emulates the most common case of the C for
loop construct, where a variable is assigned a value at the begining, then
on each next cycle updated in some way, and exited when a condition
depending on that variable evaluates to false. This function yields what
the value would be at each iteration of the for loop.
Inputs:
start: the initial yielded value
test_func: called on the previous yielded value; if false, the
the generator raises StopIteration and the loop exits.
cycle_func: called on the previous yielded value, retuns the next
yielded value
Yields:
var: the value of the loop variable
An example:
for x in cfor(0.0, lambda x: x <= 3.0, lambda x: x + 1.0):
print x # Obviously, print(x) for Python 3
prints out
0.0
1.0
2.0
3.0
"""
var = start
while test_func(var):
yield var
var = cycle_func(var)