There are two kinds of loops in Cython, for...in and for...from. This describes their specified (perhaps not yet implemented) behavior.

for i in L:
    ...

works just like python. Specifically, the variable i is not assigned to if L is empty, and is last assigned to the last value of L. If i is assigned to in the loop, the next value of i is not affected.

for i in range(...):
    ...

may be optimized to a C for loop if i is a cdef int, but the semantics will remain exactly the same. This is accomplished by using a temporary variable for the actual C loop.

for i from expr1 <= i < expr2 [by expr3]:
   ...

gets translated to

start = expr1;
end = expr2;
step = expr3;
for(i=start; i<end; i+=step) {
    ...
}

In particular:

In all cases, the bounds and step are evaluated exactly once.

loops (last edited 2009-03-23 23:41:21 by robertwb)