Why not allow empty code blocks?

Steven D'Aprano steve at pearwood.info
Fri Jul 22 21:49:36 EDT 2016


On Sat, 23 Jul 2016 01:33 am, Kent Tong wrote:

> Hi
> 
> I'm aware that we can use 'pass' as an empty code block. But why doesn't
> python allow a code block to be empty and thus eliminate the need for this
> null statement?

Because it cannot tell the difference between an empty code block and
failing to indent the code block:

for x in sequence:
print('loop')


Is that meant to print 'loop' each time around the loop, or just once, at
the end of the loop?

There are cases where the interpreter could tell:


if flag:
else:
   block


Obviously the "if" block is empty. But for consistency, and simplicity, the
interpreter requires a pass there too. One less thing to be programmed, one
less thing for the user to remember. Just require pass any time you have an
empty block, rather than try to remember where it is required and were it
is optional.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list