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


Groups > comp.lang.python > #110787

Re: "for/while ... break(by any means) ... else" make sense?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: "for/while ... break(by any means) ... else" make sense?
Date Wed, 29 Jun 2016 16:22:03 -0400
Lines 56
Message-ID <mailman.115.1467231752.2358.python-list@python.org> (permalink)
References <CAGtOLTe8VOnW6zbQSpoCwLL8Hj=cvF6wZ7wx-gxO=9-bQQ0fMw@mail.gmail.com> <nl1alp$baj$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de bIHKaFVVkKq9aOJbSr2j1g/Awo1A2FbcTUYGA5pBNNlw==
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; 'clause': 0.07; 'executed': 0.07; 'false.': 0.07; 'repeated': 0.07; '*is*': 0.09; 'bindings': 0.09; 'formatted': 0.09; 'implemented.': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:while': 0.09; 'unpacking': 0.09; 'jan': 0.11; 'def': 0.13; 'properly': 0.15; 'assignment.': 0.16; 'assigns': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'recurse': 0.16; 'reedy': 0.16; 'subject:break': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'language': 0.19; 'machine': 0.21; 'converted': 0.22; 'parameter': 0.22; 'terminate': 0.22; 'trying': 0.22; 'am,': 0.23; 'code.': 0.23; '(where': 0.23; 'needed.': 0.23; 'replacing': 0.23; 'implemented': 0.24; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'equivalent': 0.27; 'parameters': 0.27; 'values': 0.28; 'implicitly': 0.29; 'tail': 0.29; 'convert': 0.29; 'there.': 0.30; 'subject:/': 0.30; 'posts': 0.30; 'statement': 0.32; 'useful': 0.33; 'call,': 0.33; 'label': 0.35; 'but': 0.36; 'there': 0.36; 'subject:" ': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'received:org': 0.37; 'drop': 0.38; 'why': 0.39; 'goes': 0.39; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'skip:u 10': 0.61; 'above,': 0.63; 'received:96': 0.63; 'statement,': 0.66; "'while'": 0.84; 'subject:any': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'victor': 0.84; 'received:fios.verizon.net': 0.91; 'same,': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pool-96-227-207-81.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1
In-Reply-To <CAGtOLTe8VOnW6zbQSpoCwLL8Hj=cvF6wZ7wx-gxO=9-bQQ0fMw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nl1alp$baj$1@ger.gmane.org>
X-Mailman-Original-References <CAGtOLTe8VOnW6zbQSpoCwLL8Hj=cvF6wZ7wx-gxO=9-bQQ0fMw@mail.gmail.com>
Xref csiph.com comp.lang.python:110787

Show key headers only | View raw


On 6/29/2016 6:01 AM, Victor Savu wrote:
> There are many posts trying to explain the else after for or while.

My take: a while statement *is* a repeated if statement, and that is how 
it is implemented.

while condition:
     true()

is equivalent to and implemented in machine language without a native 
while command as

<label> if condition:
    true()
    goto label

An else clause is executed exactly when the condition is false.

This understanding is useful in understanding how and why tail recursion 
is equivalent to and can be converted to a while loop.

def f(n, accum):
     if recurse:
         return f(g(n), h(n, accum))
     else:
         return accum

What does the recursive call do?  It assigns new values to the 
parameters and goes to the top of the code.  Since it is a tail call, 
the old parameter bindings are not needed.  So the above does the same as

def f(n, accum):
     <label> if recurse:
         n, accum = g(n), h(n, accum
         goto label
     else:
         return accum

which is the same, by the above, as

def f(n, accum):
     while recurse:
         n, accum = g(n), h(n, accum
     else:
         return accum

In other words, convert properly formatted tail recursion (where the 
condition is the recurse condition, rather than the terminate condition) 
by replacing 'if' with 'while' and the tail recursive call with 
unpacking assignment.

It is conventional to drop the else:, but it is implicitly still there.

-- 
Terry Jan Reedy

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


Thread

Re: "for/while ... break(by any means) ... else" make sense? Terry Reedy <tjreedy@udel.edu> - 2016-06-29 16:22 -0400

csiph-web