[Python-ideas] Inline assignments using "given" clauses
Mikhail V
mikhailwas at gmail.com
Sat May 5 10:33:10 EDT 2018
On Fri, May 4, 2018 at 3:06 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> With that spelling, the three examples above would become:
>
> # Exactly one branch is executed here
> if m given m = pattern.search(data):
> ...
> elif m given m = other_pattern.search(data)):
> ...
> else:
> ...
>
> # This name is rebound on each trip around the loop
> while m given m = pattern.search(remaining_data):
> ...
>
> # "f(x)" is only evaluated once on each iteration
> result = [(x, y, x/y) for x in data if y given y = f(x)]
>
Well, I think it looks good. Nice job Nick!
I like that it leaves the "=" alone finally (but it's maybe the
reason why I am biased towards this syntax :)
It has clear look so as not to confuse with conditionals and
the variable is emphasized so it's well understood what it means.
I wish I could like the whole idea of inline assignments more now -
but well, it is just what it is. Something that can help on rare occasion.
But as said, your variant feels right to me. Unlike with most other
proposed constructs here my first impression - this is Python.
If the new keyword is too much, there are some options too choose from.
(though I've no idea whether this is plausible due to parsing ambiguities)
if m given m = pattern.search(data): vs:
if m with m = pattern.search(data):
if m def m = pattern.search(data):
if m as m = pattern.search(data):
Yes, all these reads strange, but IMO understandable.
BTW, using your keyword, in comprehensions vs the original idea
of Chris then merely I suppose:
result = [(x, y, x/y) given y = f(x) for x in data if y ] vs
result = [(x, y, x/y) with y = f(x) for x in data if y ]
result = [(x, y, x/y) def y = f(x) for x in data if y ]
result = [(x, y, x/y) as y = f(x) for x in data if y ]
vs
result = [(x, y, x/y) for x in data if y given y = f(x)] vs
result = [(x, y, x/y) for x in data if y with y = f(x)]
result = [(x, y, x/y) for x in data if y def y = f(x)]
result = [(x, y, x/y) for x in data if y as y = f(x)]
I can't say for sure what is better but the former order seems to
be slightly more appealing for some reson.
[Tim]
> it's obviously less redundant typing as:
> if match := pattern.search(data):
> In
> if match given match = pattern.search(data):
I disagree, I think it is not redundant but it's just how it is -
namely it does not introduce 'implicitness' in the first place,
and that is what I like about Nick's variant. But of course it is less compact.
PS:
recently I have discovered interesting fact: it seems one
can already use 'inline assignment' with current syntax. E.g.:
if exec("m = input()") or m:
print (m)
It seem to work as inline assignment correctly.
Yes it is just coincidence, BUT: this has the wanted effect!
- hides one line in "if" and "while"
- leaves the assignment statement (or is it expression now? ;)
- has explicit appearance
So we have it already?
Mikhail
More information about the Python-ideas
mailing list