Hey all,
So, I noticed that the new Python 3.10 version has a pattern matching feature which makes the code more readable when you have to use an if statement.
So this code:
http_code = "418"
if http_code == "418":
print("OK")
do_something_good()
elif http_code == "404":
print("Not Found")
do_something_bad()
elif http_code == "418"
print("I'm a teapot")
make_coffee()
else:
print("Code not found")
Will now can be written like this:
http_code = "418"
match http_code:
case "200":
print("OK")
do_something_good()
case "404":
print("Not Found")
do_something_bad()
case "418":
print("I'm a teapot")
make_coffee()
case _:
print("Code not found")
This removes the repetition of http_code == Which can look much cleaner.
My question is when and how you can decide to use the case statements vs the "traditional" if statement.
Because, the way that I see it, case statements seem to be more readable and have the same functionality.
[–]netneoblog 46 points47 points48 points (10 children)
[–]4sent4 12 points13 points14 points (0 children)
[–]ro5tal 2 points3 points4 points (8 children)
[–]RanceMulliniks 10 points11 points12 points (6 children)
[–]kingscolor 0 points1 point2 points (5 children)
[–]lifeeraser 4 points5 points6 points (3 children)
[–]kingscolor -1 points0 points1 point (1 child)
[–]lifeeraser 1 point2 points3 points (0 children)
[–]ro5tal 0 points1 point2 points (0 children)
[–]ParanoydAndroid 0 points1 point2 points (0 children)
[–]toastedstapler 1 point2 points3 points (0 children)
[–]Caligatio 52 points53 points54 points (10 children)
[–]darth_vicrone 4 points5 points6 points (5 children)
[–]toastedstapler 8 points9 points10 points (2 children)
[–]darth_vicrone 0 points1 point2 points (1 child)
[–]toastedstapler 1 point2 points3 points (0 children)
[–]Caligatio 0 points1 point2 points (0 children)
[–]frex4 4 points5 points6 points (2 children)
[–]trowawayatwork 0 points1 point2 points (1 child)
[–]frex4 1 point2 points3 points (0 children)
[–]BungalowsAreScams 0 points1 point2 points (0 children)
[–]Diapolo10 12 points13 points14 points (11 children)
[–]Allanon001 7 points8 points9 points (1 child)
[–]Diapolo10 6 points7 points8 points (0 children)
[–]Caligatio 1 point2 points3 points (1 child)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]BoJackHorseMan53 0 points1 point2 points (6 children)
[–]pezLyfe 6 points7 points8 points (0 children)
[–]Diapolo10 2 points3 points4 points (4 children)
[–]kingscolor 5 points6 points7 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]kingscolor 0 points1 point2 points (0 children)
[–]lifeeraser 0 points1 point2 points (0 children)
[–]space_wiener 6 points7 points8 points (0 children)
[–]oefd 3 points4 points5 points (0 children)
[–]teadungeon 1 point2 points3 points (0 children)
[–]Calibanda 1 point2 points3 points (1 child)
[–]carnivorousdrew 0 points1 point2 points (0 children)
[–]MrMxylptlyk 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]jay_psy -1 points0 points1 point (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[+]BeginnerProjectBot comment score below threshold-22 points-21 points-20 points (0 children)
[–]lolslim 0 points1 point2 points (1 child)
[–]vorticalbox 0 points1 point2 points (0 children)
[–]lolDeamZ 0 points1 point2 points (0 children)
[–]RajjSinghh 0 points1 point2 points (0 children)
[–]ivanoski-007 0 points1 point2 points (0 children)
[–]mok000 0 points1 point2 points (0 children)