On 16/07/2020 18:13, MRAB wrote:
On 2020-07-16 17:37, Steve Holden wrote:
While I understand the point of view that says that match ... : should encapsulate a sequence of indented suites, it seems to me that match/case/case/.../else has a natural affinity with try/except/except/.../finally/else, and nobody seems to think that the excepts should be indented. Or the finally. And naturally the match/else case are at the same indentation level, just as for/else, while/else and try/finally. So why, exactly, should case be indented?
My apologies for being a Bear of Very Little Brain.
[snip] For all other statement structures (if, while, try, etc.), the first line ends with a colon and the second line is indented (and is a statement). Therefore the cases should be indented.
However, for all other statement structures (if, while, try, etc.), the other parts of the structure itself (elif, else, except, etc.) aren't indented. Therefore the cases shouldn't be indented.
Either way, it's inelegant. Absolutely true. However:
I think that equal indentation suggests suites of equal status. Increased indentation suggests suites are subordinate to a previous suite. Consider these examples: (1) if..elif...else: Suites are definitely co-equal (they are alternatives, only one of which is chosen). So if/elif/else have equal indentation. (2) for...else or while...else: It's arguable IMO (YMMV); Python has chosen to indent them equally. I don't think it would have been outrageous to indent the 'else:'; one reason not to is that the 'else' might not stand out (it would typically be indented equally with the preceding line of code), unless it was allowed/forced to be indented *less than* 'for'. (3) try...except...else: IMO also arguable (at most one of 'except' and 'else' can be executed). (4) try...finally: The suites have equal status (both are always executed), so they have equal indentation. Now to me, 'case' clauses are *subordinate* to 'match'. After all, the value after 'match' applies to all the following 'case's. So I would argue in favour of indenting 'case' statements. This would make them stand out *more* (making a virtue out of 'match' *not* being followed by an indented suite). A Good Thing. One of the purposes of indentation is to make program structure clearly visible, which IMO this would do. Or from a slightly different point of view: "match...case...case..." is a single construct, a self-contained program component. Indenting the 'case's would stop them from distracting visually from other suites that are indented the same as 'match'. (I.e. other program components of equal status.) (If 'else' were allowed after 'match', I would argue that it should also be indented, for the same reasons, and because it is logically a case, albeit a special one.) Rob Cliffe