[Tutor] Losing the expressiveness of C's for-statement?/RESENDwith example

Stephen McInerney spmcinerney at hotmail.com
Tue Aug 7 12:24:18 CEST 2007


Hi Alan,

I don't deny the superiority of the underlying language design,
I'm just pointing out the very real mindjolting effect of Python not
supporting the universal syntax. Java is closer to C than Python is.
I'm bringing this up as one hurdle to migration, not a fundamental flaw.

Don't you agree that the Python tutorial should say something simple
and accessible to beginners like: "For all for-loop constructs where the
iteration can't be written as a simple range object, you probably want to
transform it to a while-loop, (or the more advanced option being a 
generator)"?
I think the tutorial is lacking on this (should I email Fred Drake?)
Instead of leaving C and Java people cold scratching their heads about
why they think the language is hopelessly quirky and not (syntactically) 
fully-featured?
One of our aims should be to write code which is at least understandable to
programmers of other languages.



>Sorry I meant to pick a tangible example to focus the discussion:
>This one cannot be (easily) translated to use Python's range()
>operator for (i=30000; i>0; i=i/2) { ... }
>You don't give us any reason why you want to generate a set
>of numbers from 30,000 down to zero decreasing by half each
>time: 30,000, 15,000, 7500, 3750, etc
>To understand the Pythonic solution to the problem we would
>need to know what those numbers were required for so that we
>could determine if another python data structure might be more
>appropriate.

Yes I did: it occurs in a quicksort as we halve the stepsize each time,
on an array of size 60000.
Can you please give me your answer on this? We have to transform it to
a while-loop? (or write a custom iterator?)
It would nice to compare the most straightforward solution (while-loop?)
the fastest solution, the last-memory solution and the most elegant 
solution.

>You have to remember that C's for loop is mostly syntactic sugar
>over a while loop: expression1 while test action expression2

Yes I know how to transform it.

>Pythons for loop is a much more powerful foreach construct
>intended to deal with collections. C's for loop is simply an extension
>of assembler syntax and deals with indices, memory addresses, numbers,
>whatever low level construct you want. The loop used for that kind
>of low level detail in Python is, as you say, the while loop.


>As to your particular case one non while option would be a generateor:
>
>def half(n):
>     while int(n) > 0:
>        n = n/2
>        yield n
>
>for x in half(300): print x,

It's ok but it's visually clunky. while-loop wins for clarity. lambda would 
also be too quirky.
I know the generator is more runtime-efficient.
It's regrettable we have to choose between the clear and the efficient, in 
this situation.

Regards,
Stephen

_________________________________________________________________
Learn.Laugh.Share. Reallivemoms is right place! 
http://www.reallivemoms.com?ocid=TXT_TAGHM&loc=us



More information about the Tutor mailing list