Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: Re: for / while else doesn't make sense Date: Fri, 20 May 2016 12:20:51 -0700 Lines: 43 Message-ID: References: <573EC62F.4090401@lucidity.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Q1duBQ4O/TIVptct7MI74waB69go8HKpnTz9tQ/REzig== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'context': 0.05; 'trailing': 0.07; 'brett': 0.09; 'optional': 0.09; 'python:': 0.09; 'recommends': 0.09; 'subject:while': 0.09; 'python': 0.10; 'exception': 0.13; 'syntax': 0.13; 'suggest': 0.15; 'instead.': 0.15; 'languages.': 0.15; '2016': 0.16; 'bug).': 0.16; 'erik': 0.16; 'gregory': 0.16; 'handling.': 0.16; 'loops': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'helper': 0.18; 'refers': 0.18; '>>>': 0.20; '(see': 0.20; 'python"': 0.22; 'programming': 0.22; 'am,': 0.23; '(or': 0.23; 'slightly': 0.23; 'feature': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'chris': 0.26; 'fri,': 0.27; 'received:17': 0.27; 'function': 0.28; 'probably': 0.31; 'received:10.0.0': 0.32; 'received:10.0': 0.34; 'desirable': 0.35; "isn't": 0.35; 'item': 0.35; 'according': 0.36; "wasn't": 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'seem': 0.37; 'version': 0.38; 'thank': 0.38; 'whatever': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'header:MIME-version:1': 0.60; 'behavior': 0.61; 'back': 0.62; 'charset:windows-1252': 0.62; 'subject: / ': 0.63; 'benefit': 0.66; '20,': 0.66; 'book,': 0.72; 'special': 0.73; 'demonstrates': 0.84; 'header:In-reply-to:1': 0.84; 'subject:else': 0.84; 'subject:sense': 0.84; 'ware': 0.91 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-05-20_05:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1605200236 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 In-reply-to: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1463772054; bh=4Ihmu9By77takAfd0mmtW3WIY0e/BNNhI7FmVEbxpjo=; h=Subject:To:From:Message-id:Date:MIME-version:Content-type; b=EhQJPbPYp6h7BQ272g/x/LJv3dWKdZrTN9FvghyLP3vc9bhD5bXBHUMMcC9ge5xPw X8zLQSKvYLRI66WicDVVKtn8GFv8QH0KagOXsIAcEY+AaVQCj+ETajT5gl/KSCgmGO 3FyHhCcvzQSJr6o15kyxe3OEkhy8sam08ROBuqTxGfVOG+AtS79e4DM31aFnvsXYnL 4lD+TAQyGge13/8CMacM3icF3iUE43ptlOhCUH7jApr2nWqEJqSFYLWQ/nxvaSiHKV JxHuCHWfLzGg06xyPLYTsVWPKsO1sKPHzGCa8Nuza9o82niMfeGbWbGeH1yToZE/ng FTD7PXoagDv3g== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <573EC62F.4090401@lucidity.plus.com> Xref: csiph.com comp.lang.python:108867 On 5/20/2016 8:59 AM, Zachary Ware wrote: > On Fri, May 20, 2016 at 3:09 AM, Erik wrote: >> On 20/05/16 00:51, Gregory Ewing wrote: >>> It's not so bad with "else" because you need to look back >>> to find out what condition the "else" refers to anyway. >> >> With my tongue only slightly in my cheek, if it was desirable to >> "fix"/clarify this syntax then I would suggest adding some optional >> (existing) trailing keywords to 'else' in this context that spells it out: >> >> for item in seq: >> if foo(item): >> break >> else if not break: >> nomatch() > With tongue firmly cheeked, you can always use the special `:#` operator: > > for item in seq: > if foo(item): > break > else:# if no break: > nomatch() > > This has the benefit that you can use whatever syntax you like after > the `:#`, and use it in any version of Python you want. According to "Effective Python: 59 Specific Ways to Write Better Python" by Brett Slatkin, Item 12 recommends against using the else block after for and while loops (see page 25): "Avoid using else blocks after loops because their behavior isn't intuitive and can be confusing." Until I read the book, I wasn't aware of this feature (or bug). Doesn't seem like a feature I would use since it's not commonly found in other programming languages. As the author demonstrates in his book, I would probably write a helper function instead. Item 13 does recommend using the else block for try/except/else/finally in exception handling. :) Thank you, Chris R.