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


Groups > comp.lang.python > #82404 > unrolled thread

Extension of while syntax

Started byNelson Crosby <nc@sourcecomb.com>
First post2014-12-11 18:21 -0800
Last post2014-12-13 22:56 +1100
Articles 15 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  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

#82404 — Extension of while syntax

FromNelson Crosby <nc@sourcecomb.com>
Date2014-12-11 18:21 -0800
SubjectExtension of while syntax
Message-ID<14d2b880-3837-4267-a769-33ae7393f78b@googlegroups.com>
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:

value = get_some_value() while value in undesired_values()

Perhaps not this exact syntax though, as the parser might try to do `value = (get_some_value() while...)` instead of `(value = get_some_value) while...`.

Other languages have features which allow something to look slightly less like this pattern, e.g. Java:

SomeType value;
while ((/* The assignment */ value = getSomeValue()) /* Compare */ == undesired_value) {}

Granted, this isn't exactly tidy, but it's a little more DRY and, IMO, preferable.

What are other's thoughts on this?

[toc] | [next] | [standalone]


#82405

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-12 13:37 +1100
Message-ID<mailman.16885.1418351865.18130.python-list@python.org>
In reply to#82404
Nelson Crosby <nc@sourcecomb.com> writes:

> I was thinking a bit about the following pattern:
>
> value = get_some_value()
> while value in undesired_values:
>     value = get_some_value()

I think that's an anti-pattern (because of the repetition, as you say).

An improvement::

    value = some_default_value_such_as_None
    while value in undesired_values:
        value = get_some_value()

More common and generally useful::

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

> What are other's thoughts on this?

I think there are already clean ways to deal with this in common use.

<URL:https://wiki.python.org/moin/WhileLoop>
<URL:https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops>

-- 
 \       “… one of the main causes of the fall of the Roman Empire was |
  `\        that, lacking zero, they had no way to indicate successful |
_o__)                  termination of their C programs.” —Robert Firth |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#82406

FromChris Angelico <rosuav@gmail.com>
Date2014-12-12 13:38 +1100
Message-ID<mailman.16886.1418351925.18130.python-list@python.org>
In reply to#82404
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

[toc] | [prev] | [next] | [standalone]


#82414

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-12-12 09:10 +0200
Message-ID<87r3w5gx7s.fsf@elektro.pacujo.net>
In reply to#82406
Chris Angelico <rosuav@gmail.com>:

> 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.

Extremely common, and not only in Python.


Marko

[toc] | [prev] | [next] | [standalone]


#82418

FromChris Angelico <rosuav@gmail.com>
Date2014-12-12 18:38 +1100
Message-ID<mailman.16895.1418369925.18130.python-list@python.org>
In reply to#82414
On Fri, Dec 12, 2014 at 6:10 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> 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.
>
> Extremely common, and not only in Python.

Something like it is certainly common, but to justify dedicated
syntax, the pure form has to be so amazingly common as to merit it. As
soon as you modify the pure form in any way, you need the statement
form, for full flexibility.

ChrisA

[toc] | [prev] | [next] | [standalone]


#82424

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-12-12 10:25 +0200
Message-ID<87iohh5l6c.fsf@elektro.pacujo.net>
In reply to#82418
Chris Angelico <rosuav@gmail.com>:

> On Fri, Dec 12, 2014 at 6:10 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Chris Angelico <rosuav@gmail.com>:
>>
>>> 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.
>>
>> Extremely common, and not only in Python.
>
> Something like it is certainly common, but to justify dedicated
> syntax, the pure form has to be so amazingly common as to merit it.

You already showed the perfect dedicated syntax.

The variations of the while-True-...-break idiom are limitless. There's
little need to "optimize" it further.


Marko

[toc] | [prev] | [next] | [standalone]


#82430

Fromcl@isbd.net
Date2014-12-12 09:09 +0000
Message-ID<qunqlb-a3o.ln1@chris.zbmc.eu>
In reply to#82414
Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
> 
> > 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.
> 
> Extremely common, and not only in Python.
> 
It's the classic C 'for' loop.

'for' in C is essentially a while with an initialiser for the loop
variable.

-- 
Chris Green
·

[toc] | [prev] | [next] | [standalone]


#82434

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-12-12 12:33 +0200
Message-ID<871to55f9h.fsf@elektro.pacujo.net>
In reply to#82430
cl@isbd.net:

> Marko Rauhamaa <marko@pacujo.net> wrote:
>> Chris Angelico <rosuav@gmail.com>:
>> 
>> > 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.
>> 
>> Extremely common, and not only in Python.
>> 
> It's the classic C 'for' loop.

It's the classic:

    for (;;) {
        ...
        if (...)
            break;
        ...
    }


Marko

[toc] | [prev] | [next] | [standalone]


#82408

From"Clayton Kirkwood" <crk@godblessthe.us>
Date2014-12-11 18:59 -0800
Message-ID<mailman.16888.1418353190.18130.python-list@python.org>
In reply to#82404
I would prefer:
while value = initial_value in undesired_values:
        value = get_some_value()

Seems I've seen something like this before, C, Perl?

Clayton


>-----Original Message-----
>From: Python-list [mailto:python-list-
>bounces+crk=godblessthe.us@python.org] On Behalf Of Ben Finney
>Sent: Thursday, December 11, 2014 6:38 PM
>To: python-list@python.org
>Subject: Re: Extension of while syntax
>
>Nelson Crosby <nc@sourcecomb.com> writes:
>
>> I was thinking a bit about the following pattern:
>>
>> value = get_some_value()
>> while value in undesired_values:
>>     value = get_some_value()
>
>I think that's an anti-pattern (because of the repetition, as you say).
>
>An improvement::
>
>    value = some_default_value_such_as_None
>    while value in undesired_values:
>        value = get_some_value()
>
>More common and generally useful::
>
>    while True:
>        value = get_some_value()
>        if value not in undesired_values:
>            break
>
>> What are other's thoughts on this?
>
>I think there are already clean ways to deal with this in common use.
>
><URL:https://wiki.python.org/moin/WhileLoop>
><URL:https://docs.python.org/3/tutorial/controlflow.html#break-and-
>continue-statements-and-else-clauses-on-loops>
>
>--
> \       “… one of the main causes of the fall of the Roman Empire was |
>  `\        that, lacking zero, they had no way to indicate successful |
>_o__)                  termination of their C programs.” —Robert Firth |
>Ben Finney
>
>--
>https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#82409

FromTerry Reedy <tjreedy@udel.edu>
Date2014-12-11 22:00 -0500
Message-ID<mailman.16889.1418353269.18130.python-list@python.org>
In reply to#82404
On 12/11/2014 9:21 PM, Nelson Crosby wrote:
> I was thinking a bit about the following pattern:
>
> value = get_some_value()
> while value in undesired_values:
>      value = get_some_value()

This is do_while or do_until.  In Python, write it as do_until in this form.

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

> 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.

The repetitiion is easily eliminated.

> value = get_some_value() while value in undesired_values()

Forget this, or anything like it, as a 'serious' proposal.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#82410

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-12 14:14 +1100
Message-ID<mailman.16890.1418354073.18130.python-list@python.org>
In reply to#82404
(Please don't top-post; instead, interleave responses inline with the
quoted material and trim the excess. See
<URL:https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.)

"Clayton Kirkwood" <crk@godblessthe.us> writes:

> I would prefer:
> while value = initial_value in undesired_values:
>         value = get_some_value()

The easy confusion between ‘=’ and ‘==’ in a condition is sufficient
that Python doesn't allow the ‘=’ operation in an expression.

I think that the subtle errors avoided by that is worth the loss of
expressions like the above.

-- 
 \          “Pity the meek, for they shall inherit the earth.” —Donald |
  `\                                              Robert Perry Marquis |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#82422

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-12-12 08:00 +0000
Message-ID<mailman.16897.1418371273.18130.python-list@python.org>
In reply to#82404
On 12/12/2014 02:21, Nelson Crosby 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:
>
> value = get_some_value() while value in undesired_values()
>
> Perhaps not this exact syntax though, as the parser might try to do `value = (get_some_value() while...)` instead of `(value = get_some_value) while...`.
>
> Other languages have features which allow something to look slightly less like this pattern, e.g. Java:
>
> SomeType value;
> while ((/* The assignment */ value = getSomeValue()) /* Compare */ == undesired_value) {}
>
> Granted, this isn't exactly tidy, but it's a little more DRY and, IMO, preferable.
>
> What are other's thoughts on this?
>

It won't happen as different format loops have been discussed and 
rejected umpteen times over the last 20 odd years, mainly because the 
code can be restructured using break as others have already pointed out. 
  Unless of course you fork Python, joining others working on variants 
such as Python 2.8 or RickedPython :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#82423

FromChris Angelico <rosuav@gmail.com>
Date2014-12-12 19:05 +1100
Message-ID<mailman.16898.1418371554.18130.python-list@python.org>
In reply to#82404
On Fri, Dec 12, 2014 at 7:00 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> It won't happen as different format loops have been discussed and rejected
> umpteen times over the last 20 odd years, mainly because the code can be
> restructured using break as others have already pointed out.  Unless of
> course you fork Python, joining others working on variants such as Python
> 2.8 or RickedPython :)

RickRolledPython, the variant in which errors become warnings because
it's never gonna give you up.

ChrisA

[toc] | [prev] | [next] | [standalone]


#82458

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-12-12 11:38 -0500
Message-ID<mailman.16919.1418402302.18130.python-list@python.org>
In reply to#82404
On Thu, 11 Dec 2014 18:21:46 -0800 (PST), Nelson Crosby <nc@sourcecomb.com>
declaimed the following:

>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.
>

	So what's wrong with

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

Closest match to Ada

loop
	value := get_some_value;
	exit when value not in undesired_values;
end loop;

(the "not in" may need to be a function receiving value and returning a
boolean, if value can be some arbitrary item -- Ada normally only allows
"in" for a range, not a list/array; though 2012 allows v1|v2|v...|vx )
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#82476

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-13 22:56 +1100
Message-ID<548c2970$0$12992$c3e8da3$5496439d@news.astraweb.com>
In reply to#82404
Nelson Crosby 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.


I agree! There are two fundamental indefinite loop structure:

- loop zero or more times
- loop one or more times

Python has syntax to support the first, namely `while`, but doesn't have
syntax to support the second.

Now it is certainly true that not everything needs to be syntax. You can
easily adjust a `while` loop to behave like a one-or-more loop. One way is
to use a sentinel value that is guaranteed to be in the undesired set:

value = something_undesired
while value in undesired_values:
    value = get_some_value()


Another way is to use an infinite loop and then break out after at least one
cycle:

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



Both of these can be good enough. A good programmer should know these
techniques because they can be generalised to "loop and a half":

while True:
    first_half()
    if condition:
        break
    second_half()


and any other semantics you might like. (This suggestions that there is, in
fact, only one *fundamental* indefinite loop: the infinite loop.)

But neither is elegant and neither reads like English pseudo-code. Pascal
has syntax for one-or-more loops, and inspired by that Python might have
had something like this:

repeat:
    <block>
until condition

That however would require two new keywords (repeat and until), and the
benefit is not enough to make it worth breaking all the code that already
uses "repeat" as a variable.


> 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:
> 
> value = get_some_value() while value in undesired_values()

That is no help at all for the general case where you have a block of code
inside the while loop. It certainly isn't worth having syntax for such a
special case.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web