Implement C's Switch in Python 3

Skip Montanaro skip.montanaro at gmail.com
Sat Feb 2 21:21:30 EST 2019


Cameron wrote:

> Skip has commented on lists being unhashable. We can elaborate on that
> if you like.
>
> However, even if you went to tuples (which would let you construct the
> dict you lay out above), there is another problem.
>
> You're looking up "x" in the dict. But the keys of the dict are not
> integers, they are lists (or tuples) or integers, so they won't match.

Yeah, I solved the immediate issue, but not the ultimate problem. I
was replying from my phone. That's my story and I'm sticking to it.
Durn smart-ass phones. They kill your attention span. I had to get
back to the cat videos. :-)

Sayth wrote:

> It seems odd with C having switch that its cleaner and more efficient than python where we are having to implement our own functions to recreate switch everytime.

Python's development team has considered the possibility of adding a
switch/case statement to Python, more than once I think. Various
alternatives have been considered and rejected. It's all documented in
PEP 3103:

https://www.python.org/dev/peps/pep-3103/

Skip


More information about the Python-list mailing list