-
Notifications
You must be signed in to change notification settings - Fork 65
Less INDENT/DEDENT? #59
Comments
Yup, it's a conundrum. Note that we debated this in #2, though perhaps not enough (also note the very last comment there). It's also mentioned in the PEP under "Use a flat indentation scheme" in "Rejected Ideas". |
The diff is actually a pretty strong argument. |
Yes, I saw it was rejected, but I wanted to make sure we considered the above args before finally rejecting it :) I missed issue #2 's comments. It does seem to be a matter of the following, and where the line is drawn:
My feeling is in practice, no one will really notice that the match statement doesn't quite follow the same indentation, because of its symmetry with if/elif/else and try/except. Then the two other args, especially the diff argument (a pragmatic reflection of symmetry) will follow. We also see similar practice in the C code of Python, at least in ceval.c, which heavily uses switch/case. PEP 7 is silent on switch specifically, but it seems to otherwise encourage braces even when not mandatory and indentation after braces. But we do see examples like this in ceval.c: case TARGET(RAISE_VARARGS): {
PyObject *cause = NULL, *exc = NULL;
switch (oparg) {
case 2:
cause = POP(); /* cause */
/* fall through */
case 1:
exc = POP(); /* exc */
/* fall through */
case 0:
if (do_raise(tstate, exc, cause)) {
goto exception_unwind;
}
break;
default:
_PyErr_SetString(tstate, PyExc_SystemError,
"bad RAISE_VARARGS oparg");
break;
}
goto error;
} which in turn is part of an enormous switch. Arguing from C usage is likely a bit of a stretch, but certainly the above convention is a practicality argument. |
I would argue in favor of the additional indentation (i.e. having case clauses be indented rather than at the same level as match), for two reasons:
|
I concur with @viridia on this. The diffs are certainly a good argument. However, only up to a certain point: the introduction of pattern matching is not primarily motivated by the need to simplify or rewrite the Python standard library. IMHO, the "Although practicality beats purity." aspect just does not seem to fully apply, because this refactoring is very short-lived and more or less a singular point in time. Conceptually, the case clauses "belong" to the match block: they implicitly refer to the expression The suggestion to make the match-statement flat effectively means to remove the end-marker from the match-statement. Although the example in C seems to also be flat, the switch-statement still has a clear end-marker Finally, I also think the grammar is much more stable, particularly since match x:
case 1:
...
case 2:
...
case (3) # <- is this still a case clause? Is the |
If That's actually a good argument for indenting or for using Personally in such cases if a small diff is really important I would resort to using a "half indent" for the newly-inserted Or ignore whitespace in the diff. |
I am fairly skeptical that the 'half-indent' strategy for minimizing indentation levels would be widely adopted, for several reasons:
And honestly I don't think it's necessary. |
I don't contest any of that. I'm just reporting what I have done in similar situations. :-) |
Arguably indenting case statements under the head match should not be done:
isinstance
for match, but of course it is not indented:Additional indentation in decision statements is a visual indication of complexity. Match statements change this assumption. And of course frequently Python developers, myself included, try to avoid increasing indentation by returning early.
Minimization of diffs is a good thing. This is especially true when we consider that we want to encourage refactoring, at the very least in the stdlib. Or the occasional need to blame. Compare the following:
versus
Looking at the evolution of the codebase with the latter diff, we are much more confident on what has happened - the new match statement has been adopted, with corresponding simplification.
The text was updated successfully, but these errors were encountered: