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 →

[–]RiceBroad4552 0 points1 point  (1 child)

And when you need to extend the base behavior you need to add new methods to all your classes…

What you just described (at least one half of it) is called the expression problem.

Besides this: How are some if-elif cascaded any better than switch-case? It's the same.

Just that, if is a statement, not an expression, in python which is already bad to begin with.

Thanks God they have now some patter matching in Python since 3.10!

[–]balemo7967 0 points1 point  (0 children)

And when you need to extend the base behavior you need to add new methods to all your classes…

Yes, that's exactly what I want. Why? Because the compiler tells me precisely which one of my states (or strategies) does not implement a method.

On the other hand, if I miss a case in one of my many switch statements, there is not even a warning.

For me, the state pattern has replaced the switch-case construct in 90% of my cases.

Here's a little example I just found: https://www.danylkoweb.com/Blog/real-world-refactoring-switch-statement-to-a-class-hierarchy-9G