3 ms·
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 (ma
by cup-of-tea 8y ago
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".
- PurpleRamen 8y agoIt'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?
- Demiurge 8y agoWhy 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.