Re: Extension of while syntax
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
On Fri, Dec 12, 2014 at 7:00 PM, Mark Lawrence breamore...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
Chris Angelico ros...@gmail.com: On Fri, Dec 12, 2014 at 6:10 PM, Marko Rauhamaa ma...@pacujo.net wrote: Chris Angelico ros...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
Marko Rauhamaa ma...@pacujo.net wrote: Chris Angelico ros...@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 · -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
c...@isbd.net: Marko Rauhamaa ma...@pacujo.net wrote: Chris Angelico ros...@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 -- https://mail.python.org/mailman/listinfo/python-list
Extension of while syntax
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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
Nelson Crosby n...@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
Re: Extension of while syntax
On Fri, Dec 12, 2014 at 1:21 PM, Nelson Crosby n...@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 -- https://mail.python.org/mailman/listinfo/python-list
RE: Extension of while syntax
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...@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 n...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
(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 c...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
Chris Angelico ros...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extension of while syntax
On Fri, Dec 12, 2014 at 6:10 PM, Marko Rauhamaa ma...@pacujo.net wrote: Chris Angelico ros...@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 -- https://mail.python.org/mailman/listinfo/python-list