views:

977

answers:

3

Which was the first version of python to include the else clause for for loops?

I find that the python docs usually does a good job of documenting when features were added, but I can't seem to find the info on this feature. (It doesn't help that 'for' and 'else' are particularly difficult terms to google for on a programming website)

+7  A: 

It's been around since at least 1.4, which is the oldest version of the documentation I know of.

Pesto
I came up with the same answer, just not quickly enough.
David Locke
+1: Quote the documentation.
S.Lott
+19  A: 

It's been present since the beginning. To see that, get the source from alt.sources, specifically the message titled "Python 0.9.1 part 17/21". The date is Feb 21, 1991. This post included the grammar definition, which states:

for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]

You might be able to find the 0.9.0 sources if you try harder than I did, but as the first public release was 0.9.0 on 20 Feb, that would get you back one day. The 0.9.1 release was a minor patch that did not affect this part of the grammar.

(Is that a UTSL reference or what? When was the last time you looked at a shar file? ;)

BTW, I reconstructed the original source and tweaked it a bit to compile under gcc-4.0 on my OS X 10.4 box. Details for those interested few, including python-0.9.1.tar.gz.

Andrew Dalke
+1  A: 

Since version 1.0.1, at least..

Python 1.0.1 (Mar 27 2009)
Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>> for x in range(2):
...     print x
... else:
...     print "loop done"
... 
0
1
loop done
dbr
http://stackoverflow.com/questions/685732/how-to-compile-python-1-0
dbr