Robert Bradshaw wrote:
> On Mar 17, 2009, at 2:35 PM, Greg Ewing wrote:
>
>> Dag Sverre Seljebotn wrote:
>>
>>> ForFromStatNode is used in two situations in Cython code; in the
>>> Cython-specific syntax
>>>
>>> cdef int i
>>> for i from start <= i < end [by step]:
>>> ...
>>>
>>> In the former case, end should NOT be frozen (i.e. if it is a
>>> function
>>> call it should be called again for every iteration
>> Has Cython changed this? It's not how the for-from
>> statement works in Pyrex -- the limits are only
>> evaluated once.
>
> No, Cython has not changed this, and I'm not sure we should. But
> there is some inconsistency:
Ahh, wasn't aware of this, I leapt to conclusions. So the way we should
go then is fix the endpoint also in for-from if it is a variable?
> %cython
> cdef int get_bound(int m):
> print "get_bound(%s)"%m
> return m
>
> def test_func(int n):
> cdef int i
> for i from 0 <= i < get_bound(n):
> print "at", i
> return i
>
> sage: test_func(5)
> get_bound(5)
> at 0
> at 1
> at 2
> at 3
> at 4
> 5
Ahh, that's strange. So the reason this happens is that the
transformation from the for-in node doesn't create the same thing that a
for-from written manually would give then... changing your code to
cdef int get_bound(int m):
print "get_bound(%s)"%m
return m
def test_func(int n):
cdef int i
for i in range(get_bound(n)):
print "at", i
return i
gives
get_bound(5)
get_bound(5)
at 0
get_bound(5)
at 1
get_bound(5)
at 2
get_bound(5)
at 3
get_bound(5)
at 4
get_bound(5)
4
Which is rather suboptimal.
--
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev