[Python-ideas] Modern language design survey for "assign and compare" statements

Rhodri James rhodri at kynesim.co.uk
Mon May 21 09:19:08 EDT 2018


On 21/05/18 13:22, Juancarlo Añez wrote:
>>    while ((v = get_something()) != INCONVENIENT_SENTINEL)
>>      do_something(v);
>>
> 
> 
> The current pattern in Python would be something like:
> 
> v = get_something()
> 
> while v != INCONVENIENT_SENTINEL:
> 
>      do_something(v)
> 
>      v = get_something()

Actually more usually

   while True:
     v = get_something()
     if v == INCONVENIENT_SENTINEL:
       break
     do_something(v)

Inelegant and, as has been pointed out, frankly misleading about the 
nature of the loop, but at least you can tell what's going on fairly 
straightforwardly.


> With "as" allowed in "while", they pattern might be:
> 
> while get_something() as v:
> 
>      if v == INCONVENIENT_SENTINEL:
> 
>          break
> 
>      do_something(v)
> 
> 
> The discussion isn't over, so it could also be:
> 
> while (get_something() as v) != INCONVENIENT_SENTINEL:
> 
>      do_something(v)

These two are somewhat different things.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-ideas mailing list