[Python-Dev] C's for statement

Guido van Rossum guido@digicool.com
Mon, 29 Jan 2001 21:15:04 -0500


[ESR]
> There's not much I miss from C these days, but one thing I wish Python
> had is a more general for-loop.  The C semantics that let you have 
> any initialization, any termination test, and any iteration you like
> are rather cool.
> 
> Yes, I realize that
> 
> 	for (<init>; <test>; <step>) {<body>}
> 
> can be simulated with:
> 
> 	<init>
> 	while 1:
> 		if <test>:
> 			break
> 		<body> 
> 
> Still, having them spatially grouped the way a C for does it is nice.
> Makes it easier to see invariants, I think.

Hm, I've seen too many ugly C for loops to have much appreciation for
it.  I can recognize and appreciate the few common forms that clearly
iterate over an array; most other forms look rather contorted to me.
Check out the Python C sources; if you find anything more complicated
than ``for (i = n; i > 0; i--)'' I probably didn't write
it. :-)

Common abominations include:

- writing a while loop as for(;<test>;)

- putting arbitrary initialization code in <init>

- having an empty condition, so the <step> becomes an arbitraty
  extension of the body that's written out-of-sequence

--Guido van Rossum (home page: http://www.python.org/~guido/)