[Python-ideas] Match statement brainstorm

Chris Angelico rosuav at gmail.com
Wed May 25 22:59:07 EDT 2016


On Thu, May 26, 2016 at 12:34 PM, Stephen J. Turnbull
<stephen at xemacs.org> wrote:
> Nikolaus Rath writes:
>
>  > Or maybe:
>  >
>  > if <thing> ...
>  >    matches <matcher>:
>  >       pass
>  >    matches <matcher>:
>  >       pass
>  >
>  > Where "..." is an actual syntactic element that replaces the ":" in a
>  > regular if.
>
> Interesting, but I don't like it because (1) ":" is *the* compound
> syntax indicator in Python, (2) "..." looks like somebody forgot to
> fill in a blank, *and it should* look that way, (3) "matches" is too
> close to "match" (which is a stdlib identifier), and it's an
> identifier I have used, and (4) it's an additional keyword.
>
> Leaving aside the spelling quibbles, following "if" I would "see"
> "matches" as an operator, and want to write
>
>     if <thing> matches <matcher>:
>         pass
>     elif <thing> matches <matcher>:
>         pass
>
> and I'm back in "if ... elif ... else ..." land.

What if it were like this:

if <thing> ...:
    matches <matcher>:
        pass
    matches <matcher>:
        pass

with a colon at the end of the first line too? (And 'matches' would
still need to be bikeshedded into oblivion.) I'm not sold on it, but
it would read reasonably well, and it still maintains the "colon then
indented block" pattern of most Python code.

ChrisA


More information about the Python-ideas mailing list