On 3/23/06, Ian Bicking <[EMAIL PROTECTED]> wrote: > Guido van Rossum wrote: > > On 3/23/06, Ian Bicking <[EMAIL PROTECTED]> wrote: [SNIP] > I'm testing if it is empty or not, which seems natural enough. Or would > be, if it worked. So I start out doing: > > for item in select_results: ... > > Then I realize that the zero-item case is special (which is common), and do: > > select_results = list(select_results) > if select_results: > ... > else: > for item in select_results:... > > That's not a very comfortable code transformation. When I was just > first learning Python I thought this would work: > > for item in select_results: > ... > else: > ... stuff when there are no items ... > > But it doesn't work like that.
I have to admit that is what I initially thought as well. I think it is because when I read 'else' I viewed it as an alternative if the clause it was attached to didn't happen (ala an 'if' statement). Obviously Python has broken me of that habit of thinking of it that way, but I bet most people are used to 'else' working like that. Would be nice to have a an easy way to specify what to do if the loop didn't execute. But that would require a keyword. Otherwise we should really include the idiom of having a boolean that gets set within the loop as part of the docs (or some Python Best Practices doc in terms of using loops or something). -Brett _______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
