[Cython] Int looping
Stefan Behnel
stefan_ml at behnel.de
Tue May 13 17:06:48 CEST 2008
Dag Sverre Seljebotn wrote:
>
>>> The 'for i from...' version was a compromise
>>
>> I understand that. Still, having two spellings for "for ... in ...", one
>> for Python, one for C, looks better than a completely different syntax
>> that just starts with "for". So I vote for
>>
>> for x in iterable:
>>
>> and
>>
>> for x in 1 < x <= 5:
>>
> Is this (int looping) something you tend to do?
It happens.
> When writing Python code
Not in Python code, but in Cython code, especially in low-level C-ish
functions.
> I almost never end up doing it, rather I end up using "enumerate" (or
> for NumPy, "ndenumerate") when I need the indices. I'd rather we worked
> on improved high-level looping than inventing new syntax for low-level
> looping.
As Greg pointed out, it's there because it's convenient.
> For instance, one could implement optimizations for "enumerate" in
> Cython as well as "range":
>
> for idx, value in enumerate(a):
> BLOCK
>
> could be turned into the much more efficient
>
> for idx from 0 <= len(a):
> value = a[idx]
> BLOCK
Provided that a is indexable, which is getting less likely in recent
Python code.
> I suppose operating with C pointers increases the need for int looping,
> but when working with C code I see a much larger need for
>
> for (char* ch = start; *ch != 0; ++ch) ...
That's a while loop.
> which could be done simply by letting our builting "enumerate" know that
> char* should stop looping when hitting 0, also the more general
> (especially in C++).
No, please. You are trying to optimise an extremely special case here,
that is best dealt with in a straight C loop.
> The more general iteration pattern (especially for C++):
>
> for (T* iter = start; iter != v.end(); ++iter) ...
>
> could be done by something like
>
> for iterator in c_iteration(a.begin(), a.end()):
> print iterator.get()
> iterator.set(3)
-1 on this and other syntax proposals.
Stefan
More information about the Cython-dev
mailing list