Conversation

giant realistic flying nachtpfoetchen

Match patterns in python seem a bit weird to me. Like why should i use match over an if statement?
2
0
0
I guess sometimes it is a bit shorter and nicer to read?
1
0
0

@nachtpfoetchen tbh i have the same feeling about case in other languages. I very rarely use it in bash, i prefer if’s and elif chains

1
0
0
@domi that matches kinda with the documentation on why it exists in python. Not that it was a something that they really needed, but something they included because it exists in other languages.
1
0
1

@nachtpfoetchen @domi because of how Python data is usually structured (not as enums, often just very basic tuples and stuff, many classes not match-compatible), it’s not very useful. Languages that have deeply structured powerful datatypes (proper sum and union types, for instance) which are match-compatible by default benefit a lot more from pattern matching, where this becomes the prevailing pattern. See Rust, Haskell and most functional languages.

0
0
0
@nachtpfoetchen match does destructuring, which is useful sometimes. Other than that it's just shorter and nicer to read.
0
0
1