Re:PEP 572: Python assignment expressions has been accepted
by cup-of-tea on Tuesday July 03, 2018 @06:06AM
(#17448929)
How would you do this?
[ Parent
| on HN ]
Re:PEP 572: Python assignment expressions has been accepted
by PurpleRamen on Tuesday July 03, 2018 @06:53AM
(#17449096)
match = re.match(r1, s)
if match:
o = match.group(1)
else:
match = re.match(r2, s)
if match:
o = match.group(1)
or a bit shorter: match = re.match(r1, s)
if not match:
match = re.match(r2, s)
if match:
o = match.group(1)
You could also just loop: for pattern in (r1, r2, ...):
match = re.match(pattern, s)
if match:
o = match.group(1)
break
else:
do_failure_handling()
But this goes a bit beyond the original question.
Re:PEP 572: Python assignment expressions has been accepted
by cup-of-tea on Tuesday July 03, 2018 @09:25AM
(#17449950)
I made a mistake in the question, it should have been:
if (match := re.match(r1, s)):
o = match.group(1)
# plus some code here
elif (match := re.match(r2, s)):
o = match.group(2)
# plus some other code here
In this case only your first solution works, I think. Leaving aside that having those deeply nested ifs is incredibly ugly, I find it hard to accept that something which completely changes the possible structure of the code is just "syntactic sugar".
Re:PEP 572: Python assignment expressions has been accepted
by PurpleRamen on Tuesday July 03, 2018 @09:59AM
(#17450207)
It's the syntactic sugars job to beautify the ugly. And your example is also a bit unfair, because you also use another syntactic sugar, the elif, to make it more beautyful. It only falls back to the ugly solution because I can't sweeten sugar for a similar dish.
But the overall question is: when is the sugar just syntactical, and at what point does it become a complete new taste?
Re:PEP 572: Python assignment expressions has been accepted
by Demiurge on Tuesday July 03, 2018 @11:58PM
(#17455182)
Why is it incredibly ugly when it actually conveys the explicit logic of that which is happening? Can the logic be simplified? Are there any unnecessary characters? I don't see that. This is why I like Python, it's easy to read, and it is easy to refactor and see how you can improve the logic.
Re:PEP 572: Python assignment expressions has been accepted
by est on Tuesday July 03, 2018 @09:09AM
(#17449827)
well, that looks easy.
m = re.match(r1, s) or re.match(r2, s)
o = m.group(1) if m else None
Re:PEP 572: Python assignment expressions has been accepted
by cup-of-tea on Tuesday July 03, 2018 @09:20AM
(#17449896)
I made a mistake in my question. The arguments to group shouldn't have been the same. What I want to get at is when the two branches are completely different code.
Re:PEP 572: Python assignment expressions has been accepted
by est on Tuesday July 03, 2018 @09:04PM
(#17454693)
I think it's a case where PEP 572 tries to fix bad library design.
re.match shouldn't return None at all. I often write helper functions like:
matcher = lambda r, s: getattr(re.match(r, s), 'group', lambda i: '')
o = matcher(r1, s)(1) or matcher(r2, s)(3)
here matcher have a fixed, static return type, string.