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


Groups > comp.lang.python > #37385

Re: Else statement executing when it shouldnt

References (3 earlier) <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 2013-01-23 01:34 +0100
Subject Re: Else statement executing when it shouldnt
From René Klačan <rene.klacan@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.848.1358901245.2939.python-list@python.org> (permalink)

Show all headers | 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