[Python-Dev] Re: PEP 622 version 2 (Structural Pattern Matching)

2020年7月11日 13:34:06 -0700

On 2020年07月11日 20:56, Steve Holden wrote:
Given that case will be a keyword, what's the case (pun unintentional) for indenting the case clauses? What's wrong with 'case' and 'else' both indented the same as match? Without the keyword there'd be a case for indenting, but with it I don't see the necessity.
Kind regards,
Steve
The issue is as follows.
In Python, the style is that multi-line statements start with a keyword and that logical line ends with a colon. The next line is a statement, which is indented. The other parts of the statement structure are indented the same amount as its first line:
 if ...:
 ...
 elif ...:
 ...
 else:
 ...
The 'match' statement (or a 'switch' statement), however, can't follow the existing style.
It's either:
 match ...:
 case ...:
 case ...:
where the second line isn't indented (unlike all other structures), or:
 match ...:
 case ...:
 case ...:
where the other parts of the structure (the cases) aren't indented the same as the first line.
Another possibility is:
 match:
 ...
 case ...:
 case ...:
but the second line is an expression, not a statement (unlike all other structures).
An alternative is:
 match ...
 case ...:
 case ...:
no colon ending the first line, and no indenting of the second line, but that's unlike all other structures too.
None of the possibilities are formatted like the existing style.
So it's a case of picking the least inelegant one.
On Fri, Jul 10, 2020 at 12:09 PM Greg Ewing <[email protected] <mailto:[email protected]>> wrote:
 A thought about the indentation level of a speculated "else" clause...
 Some people have argued that "else" should be at the outer level,
 because that's the way it is in all the existing compound statements.
 However, in those statements, all the actual code belonging to the
 statement is indented to the same level:
   if a:
     ....
   elif b:
     ....
   else:
     ....
     ^
     |
     Code all indented to this level
 But if we were to indent "else" to the same level as "match",
 the code under it would be at a different level from the rest.
   match a:
     case 1:
       ....
     case 2:
       ....
   else:
     ....
     ^  ^
     |  |
     Code indented to two different levels
 This doesn't seem right to me, because all of the cases, including
 the else, are on the same footing semantically, just as they are in
 an "if" statement.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/M4YMCRVSKVKNUXQFI5KOMT5MQH6WJ23O/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to