[Python-ideas] for/else syntax
steve at pearwood.info
Fri Oct 2 18:23:46 CEST 2009
On Sat, 3 Oct 2009 01:42:48 am Yuvgoog Greenle wrote:
> On Fri, Oct 2, 2009 at 5:35 PM, Steven D'Aprano <steve at pearwood.info>
> > I disagree that it makes sense. Why raise a SyntaxWarning for legal
> > code? Shall we raise SyntaxWarning for the following as well?
> > #1
> > pass
> > pass
> > #2
> > if False:
> > parrot()
> > #3
> > for x in :
> > do_something()
> > #4
> > try:
> > something()
> > except Exception:
> > raise
> All of your examples do exactly what they appear to do.
And so does for..else with no break.
> The for...else construct that has no "break" appears to do something,
> yet does nothing.
>>> for x in :
... print "Do nothing"
... print "Do something"
> I beg of you to try out "The Question Challenge" I
> posted earlier. It's a win-win situation for you.
Your second question:
Question #2: When would the code in the "else" block execute? If you
don't know, it's ok to guess.
If they answer "maybe when the for loop didn't run or was an empty list"
then you don't owe me anything.
But the else clause *does* run when the loop didn't run or was an empty
list. That second answer would be correct, as far as it goes. It fails
to describe all the behaviour of the else clause, but it's not wrong,
> If you're still not convinced, check out this code that is legal, yet
> appears to do something that it doesn't:
> z = math.sqrt(x / y)
> except ZeroDivisionError, ValueError:
> print("Math error, try again")
Irrelevant. We're not talking about the syntax of the except statement.
Why would confusing syntax there convince me that a *completely
different* statement was also confusing? That's like saying, "If you
don't agree that chocolate tastes terrible, have a taste of this
cow-vomit. Terrible, right? So now we agree, chocolate tastes
More information about the Python-ideas