[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 06:36:08 -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: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Matthew Gruenke (mgruenke)
>Assigned to: Guido van Rossum (gvanrossum)
>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: Guido van Rossum (gvanrossum)
Date: 2002-01-13 06:36

Message:
Logged In: YES 
user_id=6380

Even if it wasn't specified (which I don't believe -- you
probably just aren't very good at language lawyering yet
:-), it's been this way for over 10 years and would break
tons of code if it was changed.

Even if it was OK to change, I wouldn't change it because I
think the existing behavior is more useful; treating the
empty list differently would cause hidden bugs in programs.

IOW, I'm rejecting this.

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

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