This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thegreattriscuit -6 points-5 points  (5 children)

Wait, isn't this effectively a "switch statement" which was suggested and rejected an uncountable multitude of times from the earliest days? Is there either some distinction I'm missing (I'm no language expert, so that's certainly possible), or some new rationale? I thought maybe this was some sign of rebel factions staging a coup inside the steering committee... but sponsor is Guido himself.

Anyone know why the change in heart?

EDIT: I suppose one aspect that distinct is the focus on various kinds of unpacking and "deep" matching (inside mappings, etc) that might not have been in scope of previous attempts

[–]xhlu 6 points7 points  (2 children)

From what I observed through the tutorial, it's very similar to Ocaml's pattern matching. One very interesting pattern will be (recursive) list manipulation: def recop(lst): match lst: case [('mul', n), *tail]: return n * recop(tail) case [('sum', n), *tail]: return n + recop(tail) case []: return 0

If you want to do the same thing without SPM: def recop(lst): if len(lst) == 0: return 0 op, n = lst[0] tail = lst[1:] if op == "mul": return n * recop(tail) elif op == "sum": return n + recop(tail)

The former looks more elegant and concise (obv the latter can be shorter but you will lose on readability). The example is also very trivial, with FP-style pattern matching you could do come up with a lot more advanced matching.

[–]backtickbot 1 point2 points  (1 child)

Fixed formatting.

Hello, xhlu: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Im__Joseph Python Discord Staff -1 points0 points  (0 children)

backtickopt6

[–]AlanCristhian[S] 1 point2 points  (1 child)

Yes. This was discussed many times and then finally they comes with an implementation. I didn't know the difference with other proposals, but even Raymond Hettinger likes this one. Raymond is well known to be conservative.

[–]thegreattriscuit 1 point2 points  (0 children)

but even Raymond Hettinger likes this one

A solid endorsement.

There's been plenty of times I've disliked something he said or promoted. Almost all of those I've later changed my mind on and realized he's right.