[Python-bugs-list] [ python-Feature Requests-502981 ] should be able to use "else:" with "for"

noreply@sourceforge.net noreply@sourceforge.net
Sun, 13 Jan 2002 04:50:17 -0800


Feature Requests item #502981, was opened at 2002-01-13 04:46
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=502981&group_id=5470

Category: None
Group: None
Status: Open
Priority: 5
Submitted By: Matthew Gruenke (mgruenke)
Assigned to: Nobody/Anonymous (nobody)
>Summary: should be able to use "else:" with "for"

Initial Comment:
I'd like to be able to do use "else" to catch cases
where a "for" iterates over an empty list.

For example, it should behave as follows:

>>> l = [ "a", "b" ]
>>> for i in l:
...     print i
... else:
...     print "the list is empty"
...
a
b
>>> l = []
>>> for i in l:
...     print i
... else:
...     print "the list is empty"
... 
the list is empty
>>> 


As it currently functions, in Python 2.1.1, the "else"
clause always gets executed.  However, I don't think
this behavior is specified, so I'd expect it'd be
alright to change it.



----------------------------------------------------------------------

>Comment By: Matthew Gruenke (mgruenke)
Date: 2002-01-13 04:50

Message:
Logged In: YES 
user_id=343027

phoey.  The formatter tried to be intelligent, and gobbled
all my indentation.  Hopefully, you get the point, anyhow.


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=502981&group_id=5470