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


Groups > comp.lang.python > #52227

Re: beginner question (True False help)

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'cpython': 0.05; 'explicitly': 0.05; '(b)': 0.07; 'paths': 0.07; 'subject:help': 0.08; 'alternatives': 0.09; 'clause': 0.09; 'exception,': 0.09; 'exit': 0.09; 'false,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'terminated': 0.09; 'thrown': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '"if"': 0.16; 'bytecode': 0.16; 'cleanly': 0.16; 'cmp': 0.16; 'extraneous': 0.16; 'jmp': 0.16; 'jumps': 0.16; 'length.': 0.16; 'matters,': 0.16; 'paths.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'redundant.': 0.16; 'reedy': 0.16; 'return,': 0.16; 'subject:beginner': 0.16; 'true:': 0.16; 'followed': 0.16; 'language': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'programming': 0.22; 'aug': 0.22; 'putting': 0.22; 'header:User-Agent:1': 0.23; '(a)': 0.24; 'tend': 0.24; 'looks': 0.24; 'this:': 0.26; 'second': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; 'easier': 0.31; 'minor': 0.31; 'omitted': 0.31; 'trivial': 0.31; 'level.': 0.33; 'maybe': 0.34; 'could': 0.34; 'subject: (': 0.35; 'something': 0.35; '(2)': 0.35; 'etc': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'two': 0.37; 'being': 0.38; 'branch': 0.38; 'same.': 0.38; 'to:addr :python-list': 0.38; 'little': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'letters': 0.60; 'break': 0.61; 'took': 0.61; 'received:173': 0.61; 'personal': 0.63; 'such': 0.63; 'effectively': 0.66; 'determine': 0.67; 'etc),': 0.84; 'received:fios.verizon.net': 0.84; 'subject:True': 0.91; 'whereas': 0.91; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: beginner question (True False help)
Date Thu, 08 Aug 2013 16:29:00 -0400
References <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com> <72cd8537-7494-48d6-a58a-a50a319d8e2a@googlegroups.com> <CAPTjJmog1tDGqL5r7H_kum3zorA_L+NGtPiW9UqEiNL1yhCKnA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <CAPTjJmog1tDGqL5r7H_kum3zorA_L+NGtPiW9UqEiNL1yhCKnA@mail.gmail.com>
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.372.1375993759.1251.python-list@python.org> (permalink)
Lines 102
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375993759 news.xs4all.nl 16006 [2001:888:2000:d::a6]:58825
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52227

Show key headers only | 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