Python Tip #313 (of 365): Don't use match-case as a switch statement. Do this: i...

@treyhunner@mastodon.social

Python Tip #313 (of 365):

Don't use match-case as a switch statement.

Do this:
if day in ("saturday", "sunday"):
return "weekend"
else:
return "weekday"

Not this:
match day:
case "saturday" | "sunday":
return "weekend"
case _:
return "weekday"

The match version adds indentation and doesn't even rely on the "structural" part of structural pattern matching.

More on this:
pym.dev/switch-case-in-python/

๐Ÿงต (1/2)

#Python #DailyPythonTip