Hi Philip,
You wrote:
“I do think PEP-8 clearly rejects ):
tabbed in on trailing lines.”
Do you mean tabbing out here?
It can’t be that clear if we need to argue about it. If PEP 8 clearly
said something like:
“Do not tab in (out?) on ):
lines”
then there would be no room for argument. But it doesn’t, not so far as
I can see. (Perhaps I missed something?)
You wrote:
“There’s a clear difference between “}
” tabbed out and “):
” tabbed
out. And it’s this: In a dictionary the definition starts where you tab
in and ends where you tab out”
That’s incorrect. The dictionary ends at the closing brace }
,
regardless of whether you tab in, tab out, or neither.
You had this code snippet:
foo = {
# Thing has started
'x': 1,
'y': 2,
}
# Thing has finished.
(By the way, I wouldn’t write it like that. First, I would tab in by
two levels, not one, to make it clear that this isn’t a code block
indent; and second I would either align the closing tab with the
double-indented block, or place it on the same line as the final key.)
But the comment is inaccurate: the “thing” (dict) ended on the previous
line, and not because of the outdent, but because of the brace. So I
think your argument is irrelevant.
I agree with you that outdenting the brace is not visually attractive,
and I wouldn’t write it like that, but I don’t think that it has
anything to do with an outdent signalling “thing has finished” or not.
Outdenting signals the end of a block statement, not arbitrary groupings
of code such as tuples, dicts, or parameter lists in a function def
.
Since I don’t agree that this outdent “rule” applies to anything except
block statements, where it is enforced by the compiler, I don’t think
that this:
def foo(
# thing has started
bar
# thing has finished ... oh no wait...
):
breaks the rule. But I do agree that it is ugly.
For what it’s worth, the “thing” here that finishes at the colon is the
function header def funcname(parameter list):
, and the body doesn’t
start until the block beginning with an indent.