←back to thread

317 points est | 3 comments | | HN request time: 0.001s | source
Show context
gbfowler ◴[] No.17448582[source]
It is not syntactic sugar, "x := 10" is an assignment expression in contrast with "x = 10", which is a statement.

Hence the former can be used in contexts like "if x := 10: pass", which is the whole point of the PEP.

replies(2): >>17448624 #>>17448651 #
Aardwolf ◴[] No.17448624[source]
Why is it not syntactic sugar? It looks like convenience. You could argue everything above machine language is syntactic sugar.
replies(5): >>17448721 #>>17448736 #>>17448847 #>>17448880 #>>17448984 #
cup-of-tea ◴[] No.17448880[source]
Synactic sugar means the same thing can be achieved without the sugar. How could you achieve this without the new expression?
replies(1): >>17448901 #
est ◴[] No.17448901{3}[source]
by writing extra lines?

I'd prefer more lines for readability reasons.

replies(2): >>17448929 #>>17449056 #
cup-of-tea ◴[] No.17448929{4}[source]
How would you do this?

    if (match := re.match(r1, s)):
        o = match.group(1)
    elif (match := re.match(r2, s)):
        o = match.group(1)
replies(2): >>17449096 #>>17449827 #
1. est ◴[] No.17449827{5}[source]
well, that looks easy.

    m = re.match(r1, s) or re.match(r2, s)
    o = m.group(1) if m else None
replies(1): >>17449896 #
2. cup-of-tea ◴[] No.17449896[source]
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.
replies(1): >>17454693 #
3. est ◴[] No.17454693[source]
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.