Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'syntax': 0.03; 'none,': 0.05; 'say,': 0.05; 'badly': 0.07; 'clause': 0.07; 'executed': 0.07; 'python': 0.09; 'block.': 0.09; 'empty,': 0.09; 'executes': 0.09; 'worse': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.10; 'language': 0.14; '>>': 0.16; 'empty"': 0.16; 'sequence:': 0.16; 'skips': 0.16; 'statement.': 0.16; 'subject:when': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'else,': 0.17; 'skip': 0.17; 'examples': 0.18; 'jan': 0.18; 'sender:addr:gmail.com': 0.18; '(or': 0.18; 'email addr:gmail.com>': 0.20; 'runs': 0.22; 'cc:2**0': 0.23; '>': 0.23; 'branch': 0.23; 'matching': 0.23; 'nearly': 0.23; 'this:': 0.23; 'seems': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; '+0100,': 0.29; "d'aprano": 0.29; 'feature,': 0.29; 'steven': 0.29; 'thinks': 0.29; 'url:mailman': 0.29; 'no,': 0.29; 'skip:& 10': 0.29; 'knows': 0.30; 'that.': 0.30; 'url:python': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'anyone': 0.33; 'everyone': 0.33; 'times.': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'expected': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'useful': 0.36; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'url:mail': 0.40; 'your': 0.60; 'first': 0.61; 'thomas': 0.62; 'more': 0.63; 'satisfied': 0.83; "'for'": 0.84; "'while'": 0.84; '+1100': 0.84; '2013': 0.84; 'confusing': 0.84; 'dozen': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=K/7Tyks47nYz6Rsj51oq7EUUeFtpVbvD+kjLt/T/kck=; b=gx2C2WWTRkGSd+mj+hEubEpggGV8F7GgIgGLzBTyK8cRUwOhawIegey8w4e7Y/tuMu j9t4BrykdJVgGzLfWwr7WQU+zeP2JU0iSjM4mR+JN18ZEvWgl7cTdSPZL0Ou+1z3euGP 1cg4Fvq+bAoa6W2CPWqUm5Cx687ohinAT5O/OWeL7U6ZesSUO/yzvnCHJCBTxu4JxVyj CPVgFvgTcBzGIUlPtsh2ZRDJu0GqmQOeArj01p4GwKYai2xA1BmPINkdhHJhdjofMAwK QUFb8OV3Y1L/VOIMRlTuKKwGyhV5blF6SUR44qf0AjuMn6FvZzvCJZx6b2UodiD6cJlH 4Vlw== MIME-Version: 1.0 X-Received: by 10.42.46.141 with SMTP id k13mr15797586icf.46.1358901242671; Tue, 22 Jan 2013 16:34:02 -0800 (PST) Sender: porto143@gmail.com In-Reply-To: <50ff1f4c$0$29965$c3e8da3$5496439d@news.astraweb.com> References: <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com> <20130122163924.74038876@sampi> <20130122164835.74a0ebf6@sampi> <50ff1f4c$0$29965$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 23 Jan 2013 01:34:02 +0100 X-Google-Sender-Auth: z6Dh9C5cqoPysP3UnMi7IzkNjX0 Subject: Re: Else statement executing when it shouldnt From: =?UTF-8?B?UmVuw6kgS2xhxI1hbg==?= To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=90e6ba613bb4dab27604d3e9d97c Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 157 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358901245 news.xs4all.nl 6857 [2001:888:2000:d::a6]:39194 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37385 --90e6ba613bb4dab27604d3e9d97c Content-Type: text/plain; charset=UTF-8 it seems that lot of you are forgeting about this case: for i in [1,2,3,4,5]: print i else: print('this will be printed also because cycle wasnt broke') so the one case when else branch is executed is when condition is not satisfied and the other case is when there is no break executed during the cycle On Wed, Jan 23, 2013 at 12:22 AM, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > On Tue, 22 Jan 2013 16:48:35 +0100, Thomas Boell wrote: > > > On Wed, 23 Jan 2013 02:42:27 +1100 > > Chris Angelico wrote: > > > >> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell > >> wrote: > >> > Huh?! I would have expected all your examples to raise a SyntaxError > >> > or IndentationError. Why don't they? Is 'else' not required to have a > >> > matching 'if'? > >> > >> Other things can have else, including 'for' and 'while' loops. :) > > > > I must say, that's bound to be confusing for anyone who knows any > > language other than Python (or none, even). Syntax like that is "an > > accident waiting to happen"... > > Oh it's even worse than that. I reckon that nearly everyone thinks that > for...else runs the "else" clause if the for loop is empty, at least the > first time they see it. Or for the slow of thinking like me, the first > few dozen times. > > # this is wrong, but it looks right > for x in sequence: > do_something_with(x) > else: > print "sequence is empty" > > > But no, that's not what it does. `for...else` is actually more like this: > > > # this is right > for x in sequence: > do_something_with(x) > if condition: > break > else: > print "condition was never true" > > > That's right. The `else` block *unconditionally* executes after the `for` > block. The only way to skip it is to use `break`, which skips all the way > out of the combined for...else statement. > > This is a very useful feature, very badly named. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > --90e6ba613bb4dab27604d3e9d97c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable it seems that lot of you are forgeting about this case:

= for i in [1,2,3,4,5]:
=C2=A0 =C2=A0 print i
else:
=
=C2=A0 =C2=A0 print('this will be printed also because cycle wasnt= broke')

so the one case when else branch is executed is when co= ndition is not satisfied and the other case is when there is no break execu= ted during the cycle

On Wed, Jan 23, 2013= at 12:22 AM, Steven D'Aprano <steve+comp.lang.pyth= on@pearwood.info> wrote:
On T= ue, 22 Jan 2013 16:48:35 +0100, Thomas Boell wrote:

> On Wed, 23 Jan 2013 02:42:27 +1100
> Chris Angelico <rosuav@gmail.co= m> wrote:
>
>> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.in= valid>
>> wrote:
>> > Huh?! I would have expected all your examples to raise a Synt= axError
>> > or IndentationError. Why don't they? Is 'else' no= t required to have a
>> > matching 'if'?
>>
>> Other things can have else, including 'for' and 'while= ' loops. :)
>
> I must say, that's bound to be confusing for anyone who knows any<= br> > language other than Python (or none, even). =C2=A0Syntax like that is = "an
> accident waiting to happen"...

Oh it's even worse than that. I reckon that nearly everyone= thinks that
for...else runs the "else" clause if the for loop is empty, at le= ast the
first time they see it. Or for the slow of thinking like me, the first
few dozen times.

# this is wrong, but it looks right
for x in sequence:
=C2=A0 =C2=A0 do_something_with(x)
else:
=C2=A0 =C2=A0 print "sequence is empty"


But no, that's not what it does. `for...else` is actually more like thi= s:


# this is right
for x in sequence:
=C2=A0 =C2=A0 do_something_with(x)
=C2=A0 =C2=A0 if condition:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 break
else:
=C2=A0 =C2=A0 print "condition was never true"


That's right. The `else` block *unconditionally* executes after the `fo= r`
block. The only way to skip it is to use `break`, which skips all the way out of the combined for...else statement.

This is a very useful feature, very badly named.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

--90e6ba613bb4dab27604d3e9d97c--