The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Chris Angelico rosuav at gmail.com
Mon Mar 14 22:10:33 EDT 2016


On Tue, Mar 15, 2016 at 12:55 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> But of course, in Python any switch statement would have to support values
> of any and every type, not just integers. So any implementation you are
> thinking of would have to support cases like this:
>
>
> switch obj:
>     case "Hello", None: ...
>     case [1, 2, 3]: ...
>     case 23.01, 15+2j, Fraction(10, 11): ...
>     case 100**100, {}: ...
>
>
> and more. This is not negotiable: having a switch statement limited to small
> ints is simply not an option.

I agree that restricting switch to small ints is not an option;
however, is it truly an "of course" that a switch has to "support
values of *any and every* type"? IMO it would be fine to require that
they be hashable (which would cut out your list and empty dict, but
nothing else; and CPython 3.6 is already capable of seeing {1,2,3} and
using a literal frozenset, so the same could be used here). I've used
Python's dictionaries for umpteen years and only *very* occasionally
run into situations where some object can't be used as a key. I doubt
that I'd be bothered by a switch statement with the same restriction.

ChrisA



More information about the Python-list mailing list