while expression feature proposal

Tim Chase python.list at tim.thechases.com
Wed Oct 24 17:54:32 EDT 2012


On 10/24/12 16:34, Ian Kelly wrote:
> On Wed, Oct 24, 2012 at 2:40 PM, Dan Loewenherz <dloewenherz at gmail.com> wrote:
>> 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
> 
> The idiomatic way to do this is:
> 
> while True:
>     VAR = EXPR
>     if not VAR:
>         break
>     BLOCK

It may be idiomatic, but that doesn't stop it from being pretty
ugly.  I must say I really like the parity of Dan's

  while EXPR as VAR:
     BLOCK

proposal with the "with" statement.  It also doesn't fall prey to
the "mistaken-assignment vs. intentional-assignment" found in most
C-like languages.  I could see a pretty reasonable PEP coming from this.

-tkc









More information about the Python-list mailing list