[Cython] Fix for #196 (for loop bug)
Robert Bradshaw
robertwb at math.washington.edu
Sun Feb 8 03:56:08 CET 2009
On Jan 30, 2009, at 11:44 AM, Stefan Behnel wrote:
> Hi,
>
> Dag Sverre Seljebotn wrote:
>> Magnus Lie Hetland wrote:
>>> For the for-from loop, this may be intended -- as you say, it mimics
>>> the C behavior (although it seems to fly in the face of "Python
>>> intuition"): Even with the existing Cython semantics, getstep() will
>>> be called after *every iteration*.
>>>
>>> It seems to me that in
>>>
>>> for 0 <= i < n by getstep(): pass
>>>
>>> one would expect (in Python) that the step expression be evaluated
>>> only once. If that is not the case, fine. Then the for-from loop can
>>> safely be left alone as it is.
>>>
>> I think so -- the for-from loop is a C idiom from which one can
>> expect C
>> behaviour from my perspective, and if this has been the behaviour
>> so far
>> then backward compatability alone speaks against altering it. The
>> range
>> optimization has been around for much shorter and clearly should
>> behave
>> as Python range, so that story is different.
>
> I vote for a) diverging behaviour between for-range and for-from
> for the
> loop-variable after loop termination, and
+1
> b) the obvious evaluate-once
> semantics for the range() optimisation and the obvious (?)
> evaluate-on-each-step semantics for the for-from loop.
+1
> And while I'm fine with removing the emphases from the for-from
> loop in the
> docs, I really don't think we should remove it from the language.
> However,
> it needs to be stated in the docs that it follows the semantics of
> the C
> for loop as far as possible, and that the usual Python for-range
> pattern is
> preferred for readability and semantic clarity.
My thoughts exactly.
- Robert
More information about the Cython-dev
mailing list