Re:What Is the Switch-Case Equivalent in Python?
by heavenlyblue on Saturday April 03, 2021 @06:16PM
(#26685232)
You can override a comparison operator which means it’s potentially unbounded (not simple) exactly the same way as matching is.
[ Parent
| on HN ]
Re:What Is the Switch-Case Equivalent in Python?
by _ZeD_ on Sunday April 04, 2021 @02:04AM
(#26687268)
but
1) it's "opt in" (you need to explicit implement the dunder method) and
2) it's only for specific classes, you cannot change the behavior of 1 == "one"
Re:What Is the Switch-Case Equivalent in Python?
by heavenlyblue on Thursday April 15, 2021 @04:25AM
(#26817625)
But you can't do that for pattern matching either.
Re:What Is the Switch-Case Equivalent in Python?
by dragonwriter on Sunday April 04, 2021 @04:58AM
(#26687917)
> it's only for specific classes, you cannot change the behavior of 1 == "one"
More clearly because builtins are protected from monkey-patching, you can’t change the behavior of == where both operands are builtins. “Only for specific classes” implies the scope of applicability is much narrower than it is.