Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37385

Re: Else statement executing when it shouldnt

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 <porto143@gmail.com>
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; '&gt;&gt;': 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&gt;': 0.20; 'runs': 0.22; 'cc:2**0': 0.23; '&gt;': 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> <mailman.719.1358744056.2939.python-list@python.org> <c0d99c75-c82c-4456-a802-55f9bfd43240@googlegroups.com> <mailman.725.1358744831.2939.python-list@python.org> <20130122163924.74038876@sampi> <mailman.799.1358869356.2939.python-list@python.org> <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 René Klačan <rene.klacan@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.848.1358901245.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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 <rosuav@gmail.com> wrote:
> >
> >> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell@domain.invalid>
> >> 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
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:40 -0800
  Re: Else statement executing when it shouldnt Roy Smith <roy@panix.com> - 2013-01-20 23:47 -0500
  Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-21 15:52 +1100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
  Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:54 -0800
    Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 22:00 -0800
      Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-21 08:04 +0000
  Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 05:54 +0100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:06 +0100
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:07 +0100
        Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:39 +0100
          Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 02:42 +1100
            Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:48 +0100
              Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 03:07 +1100
              Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-22 23:22 +0000
                Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-23 01:34 +0100
                Re: Else statement executing when it shouldnt Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-22 22:11 -0500
              Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-22 17:28 -0800
                Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-23 12:22 +0100
                Re: Else statement executing when it shouldnt Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-23 15:35 +0200
                Re: Else statement executing when it shouldnt Jerry Hill <malaclypse2@gmail.com> - 2013-01-23 09:53 -0500
                Re: Else statement executing when it shouldnt Frank Millman <frank@chagford.com> - 2013-01-25 10:15 +0200
          Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:48 +0000
            Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:52 +0000
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
  Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-20 23:57 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-21 01:09 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:46 -0800
  Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:56 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-21 07:37 -0800
      Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-22 05:44 +1100

csiph-web