[Python-ideas] Inline assignments using "given" clauses

Sven R. Kunze srkunze at mail.de
Fri May 11 07:57:14 EDT 2018

On 11.05.2018 13:43, Nick Coghlan wrote:
> I've been thinking about this problem, and I think for the 
> If/elif/while cases it's actually possible to allow the "binding is 
> the same as the condition" case to be simplified to:
>     if command =  pattern.match(the_string):
>         ...
>     elif command =  other_pattern.match(the_string):
>         ...
>     while data = read_data():
>         ...
> Allowing this would be part of the definition of the if/elif/while 
> statement headers, rather than a general purpose assignment expression.

I can imagine that this will cover 80 to 90% of the usecases and it's 
readable as well.

> A similar enhancement could be made to conditional expressions 
> (adjusting their grammar to permit "EXPR if NAME = EXPR else EXPR") 
> and filter clauses in comprehensions (allowing "EXPR for TARGET in 
> EXPR if NAME = EXPR").

Not sure if that is too much for now. List comprehensions tend to be 
longer than expected, the same goes for the ternary expression.

Maybe, we could start with the 90% case and whether the need for more 

> In essence, "if", "elif", and "while" would all allow for an "implied 
> given" clause in order to simplify the 90% case where the desired 
> condition and the bound expression are the same.

Exactly. Maybe, even here: let's do just the 90% case. And if a lot of 
people need finer control, we can reconsider :=/given/as.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180511/dd0ee9fd/attachment-0001.html>

More information about the Python-ideas mailing list