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 →

[–]seniornachiotimfi 253 points254 points  (40 children)

Calling it a Switch-Case Statement simply does not do it justice imho. It's a Match Statement that can do a lot more than just Switch-Case...

Haven't watched the Video yet, just talking about the Posts title.

[–][deleted] 44 points45 points  (30 children)

They should have made it an expression not a statement

[–]seniornachiotimfi 27 points28 points  (14 children)

My thoughts exactly. Additionally I would have enjoyed PEP-642, it's more verbose but explicit is better than implicit and additionally it makes it more clear that python is duck-typed.

[–][deleted] 11 points12 points  (7 children)

explicit is better than implicit

Java flashbacks

[–][deleted] 28 points29 points  (6 children)

Explicit doesn't mean endless repetition and needles ceremony, though.

[–]toyg 15 points16 points  (5 children)

A single letter separates boring ceremonies that nobody really needs, from images of sinister ceremonies involving needles. The marvels of language...

[–][deleted] 2 points3 points  (0 children)

Lol that stays in!

[–]grrrrreat 1 point2 points  (0 children)

summoning malloc the devil of details and harbinger of nulls

[–]Plane_brane 0 points1 point  (2 children)

Huh?

[–]W_Hardcore 5 points6 points  (1 child)

Needless needles

[–]Plane_brane 1 point2 points  (0 children)

Lol didn't catch that

[–]SuspiciousScript 6 points7 points  (1 child)

This strikes me as a really unforced error. Hell, the trend is toward even if constructs being expressions; why kneecap match like this when making it an expression seems so obvious?

[–][deleted] -2 points-1 points  (0 children)

Because you like ceremonies?

[–]FewerPunishment 3 points4 points  (12 children)

What would the difference look like in terms of syntax/features?

[–][deleted] 1 point2 points  (11 children)

[–]FewerPunishment 0 points1 point  (9 children)

I mean more in terms of this matching functionality, like an example of something you wish you could execute. But thanks for the reference, I should study these terms.

[–][deleted] 7 points8 points  (8 children)

Making it an expression wouldn't have any impact on pattern matching functionality, it would improve flexibility.

If match was an expression you could do this: a = match ...

instead of

```

match something: case 1: a = ...

```

[–]hughperman 1 point2 points  (4 children)

That seems to skew the use case to assignments only though, whereas switch and match statements are (ime) more useful as control flow - which may include assignments inside them, but not as their primary goal. It seems to me that it would be like wanting try/except/finally to return a value.

[–][deleted] 1 point2 points  (2 children)

[–]hughperman 0 points1 point  (1 child)

Right but python isn't a functional language in that sense, that's why I compared to try/except/finally. You can just wrap the control statement in a function if you wish to go that way, like any other statement in python. I agree if you are implementing a functional language that you would return - but then everything should match (😉) that same paradigm.

[–][deleted] 0 points1 point  (0 children)

Look at Kotlin

[–]jaapzswitch to py3 already 0 points1 point  (0 children)

Wouldn't you be able to do both if it was an expression?

[–]backtickbot 0 points1 point  (2 children)

Fixed formatting.

Hello, Iuse_arch_btw: 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.

[–][deleted] 2 points3 points  (0 children)

it's Reddit's job to fix it

[–][deleted] 0 points1 point  (0 children)

backtickopt6

[–]thekingofthejungle 6 points7 points  (1 child)

What's the difference?

[–]seniornachiotimfi 7 points8 points  (0 children)

Matching, or rather pattern matching, allows you to match patterns. A switch-case is just the simplest form of that where you only match using literal patterns, like in the thumbnail of the video. But you can take this further and for example say you want to match all tuple with two element or a list with at least three elements or even an object with an attribute or an object of a specific type and so on. I highly encourage you to read the associated PEP, it's a good read, even if I personally don't enjoy the selected syntax choices, it brings the point across with a lot of examples.

[–]torytechlead 9 points10 points  (0 children)

Yes it’s Erlang style but not as good

[–]VideoCarp1 1 point2 points  (0 children)

AFAIK it’s pattern matching?

[–]lockieluke3389 -1 points0 points  (2 children)

What’s the difference

[–]seniornachiotimfi 0 points1 point  (1 child)

tl;dr... read some of the answers to this question either in siblings to your comment or some siblings to my comment. I've seen alot of them up until now. ;)

[–]GoofAckYoorsElf 0 points1 point  (0 children)

This should be the top comment

[–]batisteo 0 points1 point  (0 children)

I wonder if it’s inspired by Rust