[Cython] can we deprecate for-from loops?

Jakub Wilk jwilk at jwilk.net
Thu Oct 15 17:26:19 EDT 2015


* Stefan Behnel <stefan_ml at behnel.de>, 2015-10-11, 16:30:
>The syntax construct "for i from 0 <= i < 10" has been silently 
>outdated for years. Can we start issuing a warning that normal range() 
>loops are preferred?

Hmm. AFAICS, Cython doesn't aways optimize range() loops that well... 
For the attached example code, Cython 0.23.4 generated nice for loops 
for the for_from() function:

  for (__pyx_t_1 = 0; __pyx_t_1 < 10; __pyx_t_1++) {
    ...
    for (__pyx_t_3 = 0; __pyx_t_3 < 10; __pyx_t_3++) {
    ...

but not for the for_range() function:

  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_tuple_, NULL); ...
  ...


(The problem goes away if cdef the iteration variables, which I should 
do anyway, so it's not a big deal for me.)

-- 
Jakub Wilk
-------------- next part --------------
def for_from():
    for i from 0 <= i < 10:
        for j from 0 <= j < 10:
            print(i + j)
def for_range():
    for i in range(10):
        for j in range(10):
            print(i + j)


More information about the cython-devel mailing list