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


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

while expression feature proposal

Started byDan Loewenherz <dloewenherz@gmail.com>
First post2012-10-24 13:40 -0700
Last post2012-10-24 15:19 -0700
Articles 2 — 2 participants

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


Contents

  while expression feature proposal Dan Loewenherz <dloewenherz@gmail.com> - 2012-10-24 13:40 -0700
    Re: while expression feature proposal Paul Rubin <no.email@nospam.invalid> - 2012-10-24 15:19 -0700

#32066 — while expression feature proposal

FromDan Loewenherz <dloewenherz@gmail.com>
Date2012-10-24 13:40 -0700
Subjectwhile expression feature proposal
Message-ID<mailman.2795.1351111299.27098.python-list@python.org>
Hi all,

This is my first post to this group--I'm not subscribed, so please CC
me in responses.

So I'm sure a lot of you have run into the following pattern. I use it
all the time and it always has felt a bit awkward due to the duplicate
variable assignment.

VAR = EXPR
while VAR:
    BLOCK
    VAR = EXPR

I'm curious what the possibility of adding the following to Python as
syntactic sugar:

while EXPR as VAR:
    BLOCK

Apologies if that has been proposed before. I searched the archives
and couldn't find any mention of it.

Best,
Dan

[toc] | [next] | [standalone]


#32070

FromPaul Rubin <no.email@nospam.invalid>
Date2012-10-24 15:19 -0700
Message-ID<7xehkno6n1.fsf@ruckus.brouhaha.com>
In reply to#32066
Dan Loewenherz <dloewenherz@gmail.com> writes:
> VAR = EXPR
> while VAR:
>     BLOCK
>     VAR = EXPR

for VAR in iter(lambda: EXPR, None):
   BLOCK

where the termination sentinel might be False or '' or whatever instead
of None.  Of course if EXPR is a callable, there's no lambda.

> while EXPR as VAR:
>     BLOCK

This is kind of nice.  I wonder if it could generalize "with" somehow,
i.e. use the context manager for EXPR if it has one.  Or maybe "iter"
could be generalized so you could pass an arbutrary predicate as
termination condition, instead of a single sentinel value.

[toc] | [prev] | [standalone]


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


csiph-web