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


Groups > comp.lang.python > #52227

Re: beginner question (True False help)

From Terry Reedy <tjreedy@udel.edu>
Subject Re: beginner question (True False help)
Date 2013-08-08 16:29 -0400
References <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> <CAPTjJmog1tDGqL5r7H_kum3zorA_L+NGtPiW9UqEiNL1yhCKnA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.372.1375993759.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 8/8/2013 7:41 AM, Chris Angelico wrote:
> On Thu, Aug 8, 2013 at 7:20 AM,  <wxjmfauth@gmail.com> wrote:
>>>>> def z2():
>> ...     letters = 'abc'
>> ...     while True:
>> ...         c = input('letter: ')
>> ...         if c not in letters:
>> ...             print('end, fin, Schluss')
>> ...             break
>> ...         else:
>> ...             print('do stuff')
>
>
> Minor quibble: I don't like having a hard exit followed by an "else".

Whereas I tend to prefer to have the two alternatives cleanly marked as 
alternatives by both being indented the same.

Many alternatives are not so trivial as the above. I remember reading 
one snippet in the CPython codebase where the 'else' was omitted and the 
if clause subdivided into about three paths. It took at least a minute 
to determine that all paths terminated in such a way that there really 
was an inplied else. How much easier it would have been to read the code 
if the author had explicitly types the 'else'.

> If the "if" branch will unconditionally quit the loop (with a break,
> here, but could also be a return, a thrown exception, etc etc), I
> would prefer to see the "else" removed and its code unindented one
> level. Maybe this is just personal preference, though, learned from
> assembly language programming where a "block if" looks something like
> this:
>
> ; if x == y:
> CMP x,y
> JNZ .else
> ; Code for "x == y"
> JMP .endif
> .else:
> ; Code for "else"
> .endif
>
> Putting an unconditional departure in the "x == y" branch makes the
> "JMP .endif" redundant.

Python is not assembly ;-). 3.3 effectively ignores the extraneous 
'else:'. Either way, if the condition is false, control jumps to the 
second print. For what little it matters, the bytecode is the same length.

def f():
   while True:
     if a:
       b = 1
       break
     else:
       b = 2

 >>> dis(f)
   2           0 SETUP_LOOP              25 (to 28)

   3     >>    3 LOAD_GLOBAL              0 (a)
               6 POP_JUMP_IF_FALSE       19

   4           9 LOAD_CONST               1 (1)
              12 STORE_FAST               0 (b)

   5          15 BREAK_LOOP
              16 JUMP_ABSOLUTE            3

   7     >>   19 LOAD_CONST               2 (2)
              22 STORE_FAST               0 (b)
              25 JUMP_ABSOLUTE            3
         >>   28 LOAD_CONST               0 (None)
              31 RETURN_VALUE

def f():
   while True:
     if a:
       b = 1
       break
     b = 2

 >>> dis(f)
   2           0 SETUP_LOOP              25 (to 28)

   3     >>    3 LOAD_GLOBAL              0 (a)
               6 POP_JUMP_IF_FALSE       19

   4           9 LOAD_CONST               1 (1)
              12 STORE_FAST               0 (b)

   5          15 BREAK_LOOP
              16 JUMP_FORWARD             0 (to 19)

   6     >>   19 LOAD_CONST               2 (2)
              22 STORE_FAST               0 (b)
              25 JUMP_ABSOLUTE            3
         >>   28 LOAD_CONST               0 (None)
              31 RETURN_VALUE

-- 
Terry Jan Reedy

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


Thread

beginner question (True False help) eschneider92@comcast.net - 2013-08-07 01:17 -0700
  Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-07 09:42 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-07 13:59 -0700
    Re: beginner question (True False help) Dave Angel <davea@davea.name> - 2013-08-08 01:18 +0000
  Re: beginner question (True False help) Larry Hudson <orgnut@yahoo.com> - 2013-08-07 19:49 -0700
  Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-07 23:20 -0700
    Re: beginner question (True False help) Chris Angelico <rosuav@gmail.com> - 2013-08-08 12:41 +0100
    Re: beginner question (True False help) Terry Reedy <tjreedy@udel.edu> - 2013-08-08 16:29 -0400
      Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-09 01:05 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:27 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:05 +0100
    Re: beginner question (True False help) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-09 22:58 -0400
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:28 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:14 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:30 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:24 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:34 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 02:22 +0100
    Re: beginner question (True False help) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-10 01:40 +0000
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:40 -0700
    Re: beginner question (True False help) MRAB <python@mrabarnett.plus.com> - 2013-08-10 01:39 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:43 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 18:08 -0700

csiph-web