Cython - using a cycle from from for for

I recently read some source code from the sklearn BallTree class , which is written in Cython, and I came across some fancy syntax in a for loop:

for j from 0 <= j < n_features:
    centroid[j] += this_pt[j]

After some inspection, I can’t find the documentation for using the keyword fromin a loop for. In fact, this answer clearly indicates that the only use fromin Python is in a sentence import_from.

Although it reads strangely, my interpretation of the string is essentially:

for j in range(n_features):
    ...

... that adheres to the condition that jbegins with 0and remains smaller n_features. What exactly is the advantage of weird syntax, and does it do something different than what I expect?

+4
source share
2 answers

This is an oldie that has been maintained for compatibility with pyrex (the predecessor of cython).

for i from 0 <= i < n:

equivalently

for i in range(n):

It should be noted that it is out of date.

+4
source

This is an obsolete form.

For backward compatibility with Pyrex, Cython also supports a more detailed for-loop form, which you can find in legacy code:

for i from 0 <= i < n:

Taken from Cython docs

+3
source

Source: https://habr.com/ru/post/1628741/


All Articles