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 arises.


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.


Regards,
Sven
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to