Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82406
| References | <14d2b880-3837-4267-a769-33ae7393f78b@googlegroups.com> |
|---|---|
| Date | 2014-12-12 13:38 +1100 |
| Subject | Re: Extension of while syntax |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16886.1418351925.18130.python-list@python.org> (permalink) |
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
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