On Jan 30, 2009, at 12:56, Dag Sverre Seljebotn wrote:

> I continously come to think about more stuff here :-)

Cool :)

> I think we should leave the semantics of the for-from statement  
> alone at
> the moment, since it was introduced to mimick the C construct (and for
> all I know code may depend on the C behaviour here).


Sounds reasonable.

As for the step problem ... you wrote:

> Then self.step.result() will return a function call, and your decop  
> will invoke the function twice :-(


What you point out here is an interesting issue -- and I don't think  
it relates only to my decop. In fact, as far as I can see, the  
existing Cython behavior is quite odd here.

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. (If not, a local variable should  
probably be used here too, even with the decref out of the picture, no?)

But there seems to be a problem with range() as well:

   cdef int i, j
   cdef int getstep():
       return 2

   for i in range(0,10,2): pass
   for j in range(0,10,getstep()): pass

   print i, j
   # (10, 8)

This doesn't seem right? (I tried looking at the code generated for  
the loop with getstep, but it was a bit too hairy for me to spot the  
bug immediately :->)

On the other hand, here it seems that the right thing is done wrt. the  
use of the step expression. As opposed to in the for-from loop,  
getstep() is called only once...

Anyway... I'll have a look at adding the flag to the for-from-loop  
that will use decrementation if it's constructed from a range-loop. It  
seems that the local variable isn't an issue here (i.e. in for-from  
loops constructed from range) after all (or am I mistaken)?

I'll hold off on adding a local variable to the plain for-from loop  
for now (and as yet I have no idea what's up with the 10 vs 8 above),  
as I don't know if the repeated evaluation of the step expression is  
desired.

-- 
Magnus Lie Hetland
http://hetland.org


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to