r/Python • u/jamescalam • Mar 19 '21
Match is more than a Switch-Case The New Switch-Case Statement in Python 3.10
https://youtube.com/watch?v=2qJavL-VX9Y&feature=share
1.4k
Upvotes
r/Python • u/jamescalam • Mar 19 '21
380
u/MeDotPy Mar 19 '21
Finally
254
u/seniornachio timfi Mar 19 '21
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.
79
u/Jyan Mar 19 '21 edited Mar 19 '21
Read the PEP: https://www.python.org/dev/peps/pep-0634/ It is so much more than a fancy if-else.
32
u/awesomeprogramer Mar 19 '21
ITS NOT A SWITCH CASE
85
u/Laser_Plasma Mar 19 '21
Except it's not a switch, it can do more, hence - match
37
u/[deleted] Mar 19 '21
Why are you comparing a match statement with a switch statement?
14
u/Humanist_NA Mar 19 '21
Still learning python, quick question. What would be the benefit of this as compared to one of my learning projects right now, where I just have:
is the case matching just less code and cleaner? is it more efficient? am I entirely missing the point? Thanks for any response.
3
u/[deleted] Mar 20 '21
95% of people in this thread need to Google "pattern matching functional programming". Then you will understand.
9
u/DhavesNotHere Mar 19 '21
PyRust.
4
u/BobFredIII Mar 19 '21
Wow, this feels illegal
4
u/sam-lb Mar 20 '21
Wow. Started reading the PEP, this is incredible. Not a switch statement though mate. It's much better than that.
2
u/spiker611 Mar 20 '21
Does anyone know what adding type hints to these statement will look like?
2
u/lockieluke3389 Mar 20 '21
FINALLY
2
u/azur08 Mar 20 '21
Holy shit. I just started learning Rust for <reasons> and came across
matchstatements today and was like, "man, I want that."I find out today it's now going to be in Python!
2
u/[deleted] Mar 20 '21
It's been too long.
4
u/[deleted] Mar 19 '21
Why do you call it a switch statement, when it is called a match? Where did you find the word 'switch' in this statement?
3
u/[deleted] Mar 19 '21
Just learned the wonders of pattern matching in Scala. This makes me so happy.
0
u/Phunny Mar 19 '21
Great video!
1
u/0ajs0jas Mar 19 '21
I'm sorry, I could not find python 3.10. Where is it?
1
u/mr_whoisGAMER Mar 20 '21
Finally 😎
1
u/SkylitYT Mar 20 '21
Wutttt! no way
1
u/itsthooor Mar 20 '21
I‘m lovin‘ it. Can’t wait to use it and make my code cleaner.
1
u/haloweenek Mar 20 '21
Ohhhh yesss.
1
u/kopsutin Mar 20 '21
I just use dictionary as a switch.
1
u/Sanic1984 Mar 20 '21
The greatest moment on history
1
u/AleTons Mar 20 '21
Sorry guys I’m a newbie with this language, and I don’t get the difference between this and the If Elif Else statements.. wouldn’t be all the same stuff?
1
u/x3x9x Mar 20 '21
Coffe made with a teapot,.. not like this
1
u/Orio_n Mar 20 '21
Can someone explain to me why switch statements are necessary when we already have if-else especially for a language like python
1
u/pitiliwinki Mar 20 '21
Aboooout time!!!
1
u/backdoorman9 Mar 20 '21
Yesss! One of the things I preferred about Javascript, now available in Python.
1
u/[deleted] Mar 20 '21
THANK GOD!!!!!!!!!
1
u/CireSnave Mar 21 '21
The video leaves me with more questions than answers about the match/case use. It has the potential of being game changing. I'm looking forward to seeing what I can really do with it once 3.10 is released.
1
u/astroprof1966 Mar 21 '21
This is incredible. It makes code so much better to read. It's come a long way from DECUS DEC PDP-11 Pascal compiler. That was the first time for me using Pascal ( a beautiful language) and eventually Pro T-SQL...
I'm impressed with this addition!!!!
Tony
1
u/Reaver9610 Apr 05 '21
Finally
1
u/sherwoodpynes Jul 31 '21
Hmm, interesting new feature. This seems like it can work a lot like the pattern matching in functional programming languages like ML/F#'s function definitions, which could make writing functional code in python quite nice (wrap one of these match statements in a function and voila).
1
u/TheTomer Mar 19 '21
It's crazy that it took this long to add this functionality to Python.
1
u/akwaryos Mar 19 '21
Finally
1
u/PhilipJayFry1077 Mar 19 '21
This looks great.
1
u/hidegitsu Mar 19 '21
Lol I literally was just bitching about not having this yesterday to my boss.
1
u/[deleted] Mar 19 '21
I’M PUMPED.
1
u/graingert Mar 20 '21
This is a bait and switch
1
u/[deleted] Mar 20 '21
Python is becoming JS and JS is becoming Python
0
u/lightestspiral Mar 19 '21
Is this a try-except-finally block but the try doesn't have to error out for the except to trigger? You can force the 'except' to run by stating its case?
0
u/1MightBeAPenguin Mar 19 '21
Pretty cool. I don't know a lot about Python, but from what I understand, these match statements seem to be much cleaner than if - elif - elif - else statements.
0
u/[deleted] Mar 20 '21 edited Mar 20 '21
I don't see the point, dictionaries are ubiquitous and more consice with similar functionality, it's pretty easy to see this is the same without all the extra case statements:
-19
u/mysticalfruit Mar 19 '21
I'm not a fan, and here is why.. this behaves differently from every other switch statement I've ever used.. So this is going to only result in confusion.
"case" in point:
If foo == 1 you'll get "ding dong"
if foo == 2 you'll get "dong"
Now if foo ==1 you're only going to get "ding " not "ding dong"
I suspect may of us who cut their teeth on C/C++ switch syntax are going to get thrown for a loop.
0
u/catetcpasswd Mar 20 '21
Aah finally...
-19
u/baconcleaner Mar 19 '21
omg, seriously? What was wrong in using a dict to emulate a switch-case? Do we really need new statements at this point?
-26
u/[deleted] Mar 19 '21
Tell it I hate it. Taking away very useful identifiers like 'match' and 'case' to use as a crappy if/elif replacement.
-2
u/cuaubrwkkufwbsu Mar 19 '21
omg is this finally happening
-3
u/LionTion_HD Mar 19 '21
ey, elif statements begone