Why not allow empty code blocks?
BartC
bc at freeuk.com
Sun Jul 24 06:35:48 EDT 2016
On 23/07/2016 16:55, Steven D'Aprano wrote:
> On Sun, 24 Jul 2016 12:06 am, BartC wrote:
>
>> pass can only do so much. If doesn't help here:
>>
>> for x in sequence:
>> print("Something")
>> print("Something else")
>>
>> Was the second print meant to be indented as well or not?
>
> True. But once you start wondering about code the programmer *hasn't*
> written, you could drive yourself crazy:
>
> ...but what if the second print is supposed to follow
> an "if" clause, which has been left out...?
I didn't want to get into this subject again, but Python's indentation
scheme is fragile.
Given an otherwise correctly typed program that compiles with no errors,
then it is very easy (if Backspace or Delete is inadvertently pressed
for example), for an indent to disappear without your noticing, but a
program still compiles. And still runs without execution errors. But
might now be subtly wrong.
Deleting any other character than a leading space or tab I think is more
likely to result in an error that would be noticed, generate a compile
error, or execute error ('variable not initialised'), or that goes wrong
more obviously.
>> Perhaps rather than 'pass', the language ought to have provided an
>> optional 'end' keyword to mark the end of a block. Then there would be
>> less speculation about what was meant:
>
> It can't be optional. If it were optional, we'd be no better off:
>
> for x in sequence:
> print(x)
But that's a choice. Being optional, it means someone /can/ make use of
it...
> The "end" is missing, but is it missing from the end of the empty block, or
> the end of the non-empty block?
>
> for x in sequence:
> end
> print(x)
>
> for x in sequence:
> print(x)
> end
...then it might look like the first example above. The second would be
an error.
>
> In any case, using "end" instead of "pass" is a poor tradeoff. Instead of
> needing to use "pass" (say) one time in a thousand when it is needed, you
> would need to use "end" 999 times in a thousand when it *isn't* needed.
Yes, well, some of us wouldn't mind! But my suggestion is simply that
you can write:
for x in sequence:
end
instead of:
for x in sequence:
pass
The first now allows statements to be added or removed from the body of
the loop without needing to change the 'end'; it wouldn't look out of
place as a trailing 'pass' would.
But thinking about it some more, it wouldn't work. All the blocks that
don't now use 'end' would look odd. I think it would either have to be
all or nothing. I guess nothing.
--
Bartc
More information about the Python-list
mailing list