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

Smith, Jeff jsmith at medplus.com
Tue Aug 7 15:00:33 CEST 2007

From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Stephen McInerney

From: alan.gauld at btinternet.com; tutor at python.org
>>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.

I would have to disagree completely.  Knowing both C and Python
intimately now, I find this Pythonic expression far easier to read than
the overly-verbose C statement.  The nice thing about python is that it
makes chunking much easier than C which increases readability.  Remember
that these lines would be visually separated.  In the algorithmic part
of the quicksort all you would have is

fox x in decrement_by_halves(300):
	do stuff

Note that I changed the name of the generator to be more explicit.  The
loop now tells exactly what is being generated.  No more tearing apart
an ugly C for construct which forces implementation of the generator at
a place where you don't really need to know about it.


More information about the Tutor mailing list