[Python-Dev] Simple Switch statement

Michael Urman murman at gmail.com
Mon Jun 26 15:15:08 CEST 2006


On 6/25/06, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
> Those were not empty words.  I provided two non-trivial worked-out examples
> taken from sre_constants.py and opcode.py.  Nick provided a third example from
> decimal.py.  In all three cases, the proposal was applied effortlessly resulting
> in improved readability and speed.  I hope you hold other proposals to the same
> standard.

I appreciate your attempts to help us avoid overengineering this so
I'm trying to find some real world examples of a pygame event loop
that really show the benefit of supporting named constants and
expressions. I may mess up irrelevant details, but the primary case
looks something like the following (perhaps Pete Shinners could point
us to a good example loop online somewhere):

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN: ...
        elif event.type == pygame.KEYUP: ...
        elif event.type == pygame.QUIT: ...

Here all the event types are integers, but are clearly meaningless as
integers instead of an enumeration. I'd be sorely disappointed with
the addition of a switch statement that couldn't support this as
something like the following:

    for event in pygame.event.get():
        switch event.type:
        case pygame.KEYDOWN: ...
        case pygame.KEYUP: ...
        case pygame.QUIT: ...

I'd also generally like these to be captured like default values to
function arguments are. The only argument against this that stuck with
me is over the fact that locals cannot be used. If literals-only has a
chance, than I would hope that every hashable non-local capturable
expression should be at least as welcome. In summary I'm +0 on switch,
but -1 on literal-only cases.

I also would like to see a way to use 'is' instead of (or inaddition
to) '==' for the comparison, but I don't have any use cases behind
this.

Michael
-- 
Michael Urman  http://www.tortall.net/mu/blog


More information about the Python-Dev mailing list