Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: empty clause of for loops Date: Wed, 16 Mar 2016 13:16:57 +0100 Lines: 54 Message-ID: References: <56E93413.6090108@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de UBtPsPQkoPcRESxfgoTE4QJWHz8gIJRl+3+NdKARci4w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'patterns': 0.04; 'although,': 0.09; 'before.': 0.09; 'python': 0.10; ':-)': 0.12; 'def': 0.13; 'delighted': 0.16; 'keyword.': 0.16; 'language:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'try:': 0.18; ';-)': 0.18; 'solution.': 0.18; 'variable': 0.18; 'thanks.': 0.18; 'language': 0.19; 'pass': 0.22; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'yield': 0.27; 'raise': 0.29; "i'm": 0.30; 'class': 0.33; 'except': 0.34; 'received:10.0': 0.34; 'best,': 0.35; 'false': 0.35; 'item': 0.35; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'missing': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'charset:windows-1252': 0.62; 'more': 0.63; 'course.': 0.67; 'filling': 0.76; 'otten': 0.84; 'approach.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1458130618; bh=JEk1fg4vRo7RRPr4G/yl+cUNDhOlPNHaxxEUSvVjTdI=; h=Subject:To:References:From:Date:In-Reply-To:From; b=pmkUT4gyWUL4CiKN38eU1ywsxPe3rllCnzvp9/TwtMAW4rE6YfdVlKn+geBzz23UN DI/1KQ4TLPKhfG4XVqGoBnhNg1L4ToBMuBzIrF4un1FhSfmnvIZNaPY5cIwwCRSnYG sKlf6mUh330jjbYWA9cAjMkWkWiv/KTQ2E3TcbRk= In-Reply-To: X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 1211 X-purgate-ID: 154282::1458130618-000018ED-937F1211/0/0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105012 On 16.03.2016 11:47, Peter Otten wrote: > > What would you expect? A keyword filling the missing functionality? Some Python magic, I haven't seen before. ;-) > >>>> class Empty(Exception): pass > ... >>>> def check_empty(items): > ... items = iter(items) > ... try: > ... yield next(items) > ... except StopIteration: > ... raise Empty > ... yield from items > ... >>>> try: > ... for item in check_empty("abc"): print(item) > ... except Empty: print("oops") > ... > a > b > c >>>> try: > ... for item in check_empty(""): print(item) > ... except Empty: print("oops") > ... > oops He will be highly delighted so see such a simplistic solution. ;-) > I'm kidding, of course. Keep it simple and use a flag like you would in any > other language: > > empty = True: > for item in items: > empty = False > ... > if empty: > ... > He likes this approach. Thanks. :-) Although, I for one would like a keyword. I remember having this issue myself, and found that the "empty" variable approach is more like a pattern. As usual, patterns are workarounds for features that a language misses. Best, Sven