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


Groups > comp.lang.python > #105025

Re: empty clause of for loops

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ruud de Jong <ruud.de.jong@xs4all.nl>
Newsgroups comp.lang.python
Subject Re: empty clause of for loops
Date Wed, 16 Mar 2016 14:37:00 +0100
Lines 37
Message-ID <mailman.202.1458135923.12893.python-list@python.org> (permalink)
References <56E93413.6090108@mail.de> <ncbdk4$h9u$1@ger.gmane.org> <56E94EB9.9050507@mail.de> <ncbl8p$hq6$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de 2JA5OFhDQB1c52bdzDYLpQXl8AxuFKReklAb6kUKalFA==
Return-Path <ruud.de.jong@xs4all.nl>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'finished,': 0.07; 'considered.': 0.09; 'empty,': 0.09; 'throw': 0.09; 'python': 0.10; 'syntax': 0.13; 'empty.': 0.16; 'from:addr:xs4all.nl': 0.16; 'hypothetical': 0.16; 'object()': 0.16; 'received:194.109': 0.16; 'received:194.109.24': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'received:xs4all.nl': 0.16; 'sequence:': 0.16; 'language': 0.19; 'exceptions': 0.22; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'room': 0.27; 'least': 0.27; 'question': 0.27; 'cool': 0.27; 'sequence': 0.27; 'function': 0.28; 'implement': 0.32; 'reference,': 0.33; 'handle': 0.34; 'list': 0.34; 'something': 0.35; 'item': 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'assigned': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'your': 0.60; 'received:194': 0.61; 'disclaimer:': 0.67; 'received:nl': 0.72; "'for'": 0.84; '8.3': 0.84; 'answer:': 0.84; 'conceived': 0.84; 'otten': 0.84; 'schreef': 0.84; 'syntax:': 0.84; 'severe': 0.91
In-Reply-To <ncbl8p$hq6$1@ger.gmane.org>
X-Sender ruud.de.jong@xs4all.nl (Cg5e4CFEQvk1Gh4bEcdc1Wvmx4ozU3vK)
User-Agent XS4ALL Webmail
X-Mailman-Approved-At Wed, 16 Mar 2016 09:45:22 -0400
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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>
Xref csiph.com comp.lang.python:105025

Show key headers only | View raw


Peter Otten schreef op 2016-03-16 13:57:
> If you don't like exceptions implement (or find) something like
> 
> items = peek(items)
> if items.has_more():
>    # at least one item
>    for item in items:
>        ...
> else:
>    # empty
> 
> Only if such a function is used a lot or cannot be conceived without 
> severe
> shortcumings adding to the syntax should be considered. The 
> (hypothetical)
> question you should answer: which current feature would you throw out 
> to
> make room for your cool new addition?

No need for hypothetical functions or syntax:

x = sentinal = object()
for x in sequence:
    # handle x
if x is sentinal:
    # sequence was empty.

Disclaimer: not tested because I don't have access to Python from my 
working location.
But it should work according to the language reference, section 8.3 on 
the 'for' statement:
"Names in the target list are not deleted when the loop is finished, but 
if the sequence is empty, they will not have been assigned to at all by 
the loop."

Regards, Ruud

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


Thread

Re: empty clause of for loops Ruud de Jong <ruud.de.jong@xs4all.nl> - 2016-03-16 14:37 +0100

csiph-web