Dennis Lee Bieber wrote:

On Wed, 09 Feb 2005 18:10:40 -0800, Jeff Shannon <[EMAIL PROTECTED]>
declaimed the following in comp.lang.python:


for i in range(n)[::-1]:
    func(n)


        Shouldn't that be
                func(i)
(the loop index?)

You're right, that's what I *meant* to say. (What, the interpreter doesn't have a "do what I mean" mode yet? ;) )


The '[::-1]' iterates over the range in a reverse (decreasing) direction; this may or may not be necessary depending on the circumstances.

Eeee.... sneaky... (I'm a bit behind on latest syntax additions)

        I'd probably have coded something like

for n1 in range(n):
        func(n-n1)

though, and note that I do admit it here [...]

Given a need/desire to avoid extended slicing (i.e. being stuck with an older Python, as I often am), I'd actually do this by changing the input to range(), i.e.


    for i in range(n, 0, -1):   # ...

That (IMO) makes the decreasing-integer sequence a bit clearer than doing subtraction in the function parameter list does. Actually, it's possibly clearer than the extended slicing, too, so maybe this would be the better way all around... ;)

I haven't done the detailed
analysis to properly set the end point...

And as Peter Hansen points out, none of the Python versions leave n in the same state that the C loop does, so that's one more way in which an exact translation is not really possible -- and (IMO again) further evidence that trying to do an exact translation would be ill-conceived. Much better to consider the context in which the loop is used and do a looser, idiomatic translation.


Jeff Shannon
Technician/Programmer
Credit International

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to