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


Groups > comp.lang.python > #82406

Re: Extension of while syntax

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'modify': 0.07; 'form?': 0.09; 'shifting': 0.09; 'subject:while': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'limit,': 0.16; 'partly': 0.16; 'recognise': 0.16; 'repetition,': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'looks': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'possibly': 0.26; 'header:In-Reply- To:1': 0.27; 'wondering': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'usually': 0.31; 'this.': 0.32; '(e.g.': 0.33; 'fri,': 0.33; 'maybe': 0.34; "i'd": 0.34; 'could': 0.34; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'similar': 0.36; 'list': 0.37; 'being': 0.38; 'fact': 0.38; 'pm,': 0.38; '12,': 0.39; 'sure': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'is.': 0.60; 'break': 0.61; 'kind': 0.63; 'skip:n 10': 0.64; 'due': 0.66; 'prompt': 0.68; 'idiom': 0.84; 'rework': 0.84; 'subject:Extension': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=/A+IyUOGl9vxWzKG+iKAxM7esqvr28YIAL8fTUOY2Wo=; b=XcVH9eBYWAASobPZF5C4kNzalICYQ48ulYSqkajX9U1VBA6fKrAQ2wzprJxE7gBiJc ySeVv12wNX2FSRncES3Icq9+hOr2kyPzfHHwfFGsEn6YgwFUAOc2nyGT3X9m+/Gom/yB n4VZILOU0y0BmRbw/+w4oZC0jOa83k/3/nhUnn/45+9qDCpW3069vIcLODR8Vn/r8Tuy 5j/PL7Ohh9V3J7INiucS1JYPW53vkQ8t8DU8ph0UlY0u93VZdAyX8BZkr23aDaTjE4KW /rMnry6DkuGUaFr3JwUXPLAhGmjt27q4L8yl6Agwy/LGdRmh+PWgKxyI56+pIABOXNQE Y6NQ==
MIME-Version 1.0
X-Received by 10.112.166.101 with SMTP id zf5mr13471004lbb.42.1418351922827; Thu, 11 Dec 2014 18:38:42 -0800 (PST)
In-Reply-To <14d2b880-3837-4267-a769-33ae7393f78b@googlegroups.com>
References <14d2b880-3837-4267-a769-33ae7393f78b@googlegroups.com>
Date Fri, 12 Dec 2014 13:38:42 +1100
Subject Re: Extension of while syntax
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.16886.1418351925.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418351925 news.xs4all.nl 2892 [2001:888:2000:d::a6]:59154
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82406

Show key headers only | View raw


On Fri, Dec 12, 2014 at 1:21 PM, Nelson Crosby <nc@sourcecomb.com> wrote:
> I was thinking a bit about the following pattern:
>
> value = get_some_value()
> while value in undesired_values:
>     value = get_some_value()
>
> I've always hated code that looks like this. Partly due to the repetition, but partly also due to the fact that without being able to immediately recognise this pattern, it isn't very readable.
>
> Python already has one-line syntaxes (e.g. list comprehensions), I was wondering what people thought about a similar thing with while. It might look something like:

You could deduplicate it by shifting the condition:

while True:
    value = get_some_value()
    if value not in undesired_values: break

But I'm not sure how common this idiom actually is. Usually I'd modify
it, maybe with an iteration limit, or possibly some kind of prompt to
the human. Do you really have this perfectly pure form?

You could rework it to use filter(). I'm not sure that this is in any
way better code, but it is different...

>>> def get_some_value():
    return int(input("Enter a value: "))
>>> undesired_values = {1, 2, 3}
>>> next(filter(lambda x: x not in undesired_values, iter(get_some_value, object())))
Enter a value: 3
Enter a value: 1
Enter a value: 2
Enter a value: 1
Enter a value: 3
Enter a value: 1
Enter a value: 2
Enter a value: 7
7
>>>

Look, ma! No duplication!

ChrisA

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


Thread

Extension of while syntax Nelson Crosby <nc@sourcecomb.com> - 2014-12-11 18:21 -0800
  Re: Extension of while syntax Ben Finney <ben+python@benfinney.id.au> - 2014-12-12 13:37 +1100
  Re: Extension of while syntax Chris Angelico <rosuav@gmail.com> - 2014-12-12 13:38 +1100
    Re: Extension of while syntax Marko Rauhamaa <marko@pacujo.net> - 2014-12-12 09:10 +0200
      Re: Extension of while syntax Chris Angelico <rosuav@gmail.com> - 2014-12-12 18:38 +1100
        Re: Extension of while syntax Marko Rauhamaa <marko@pacujo.net> - 2014-12-12 10:25 +0200
      Re: Extension of while syntax cl@isbd.net - 2014-12-12 09:09 +0000
        Re: Extension of while syntax Marko Rauhamaa <marko@pacujo.net> - 2014-12-12 12:33 +0200
  RE: Extension of while syntax "Clayton Kirkwood" <crk@godblessthe.us> - 2014-12-11 18:59 -0800
  Re: Extension of while syntax Terry Reedy <tjreedy@udel.edu> - 2014-12-11 22:00 -0500
  Re: Extension of while syntax Ben Finney <ben+python@benfinney.id.au> - 2014-12-12 14:14 +1100
  Re: Extension of while syntax Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-12-12 08:00 +0000
  Re: Extension of while syntax Chris Angelico <rosuav@gmail.com> - 2014-12-12 19:05 +1100
  Re: Extension of while syntax Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-12-12 11:38 -0500
  Re: Extension of while syntax Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-13 22:56 +1100

csiph-web