Why doesn't Python have switch-case?

前端 未结 4 1303
遇见更好的自我
遇见更好的自我 2021-01-30 08:17

Please explain why Python does not have the switch-case feature implemented in it.

4条回答
  •  臣服心动
    2021-01-30 09:05

    I remembered in ancient time, an inexperienced Larry Walls said that Perl doesn't need case switch construct because it can be done the same way with: "if - elif - elif .... else". Back then Perl was nothing more but a mere scripting tool for hacker kiddies. Of course, today's Perl has a switch construct.

    It's not unexpected that over some decades later, the new generation kids with their new toys are doomed to repeat the same dumb statement.

    It's all about maturity, boys. It will eventually have a case construct. And when python has matured enough as a programming language, like FORTRAN/Pascal and C and all languages derived from them, it will even have a "goto" statement :)

    BTW. Usually, case switch translated to asm as indirect jump to list of address of respective cases. It's an unconditional jump, means far more efficient than comparing it first (avoiding branch misprediction failure), even in just a couple cases it considered as more efficient. For a dozen or more (up to hundreds in code snippet for a device driver) the advantage of the construct is unquestionable. I guess Larry Walls didn't talk assembly back then.

提交回复
热议问题