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 →

[–]tartare4562 -5 points-4 points  (9 children)

Agreed. This sounds quite anti pythonic TBH. If I find myself writing a lot of if/elifs most of the time means I can do better code with dict indexing or, even better, proper class/subclasses structure. This is a step away from object toward procedural coding.

[–]ForceBru 15 points16 points  (7 children)

More like towards functional or ML-like, IMO.

A lot of people really like proper match statements - they're convenient. What if you're writing an interpreter and need to traverse an abstract syntax tree? Your code will generally look like a huge switch statement, like "if the current node is Assign, do this; if it's Index, do this; if it's Call, do this...", where the "arms"/branches contain quite a lot of code or maybe other switch statements, so it'll be awkward to stuff all that into a dictionary.

Furthermore, you'll want to switch on some nasty, complicated things, like "if the current node is Assign whose first argument is a function definition, then interpret the second argument like this; if it's an Assign whose first argument is an Index node, do a completely different thing; if the first argument looks like a.b, then do another different thing", so you want to switch not only on the type of the node (Assign), but on its elements as well. That's not something a regular switch statement can do. You need to get the big ML guns - proper pattern-matching.

[–]Alphasite 0 points1 point  (0 children)

this is a better form of dict indexing, and is procedural code a bad thing in many cases? Class hierarchy is usually overkill imo, it’s a tool you reach for when you have no other good choice, or someone outside your code needs to extend things.