Why not allow empty code blocks?

D'Arcy J.M. Cain darcy at Vex.Net
Sat Jul 23 08:29:47 EDT 2016


On Sat, 23 Jul 2016 14:13:46 +0300
Marko Rauhamaa <marko at pacujo.net> wrote:
> > 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?
> 
> I don't see an ambiguity: obviously the print call takes place after
> finishing the loop.

It's ambiguous to the reader what was actually meant.  Forcing a "pass"
there shows the reader that the empty block was not a mistake.
Explicit is better than implicit.

> I wonder if there is any true case of ambiguity. I guess this is all
> about an enforced aesthetic principle: GvR doesn't like the looks of
> an empty block.

I don't think that he would be alone.

> Actually, the requirement of a dummy statement is a slight annoyance
> for the programmer. After deleting a statement, you must see if you
> have to put in a pass statement. And after adding a statement, you
> may feel the urge to remove the redundant pass statement.

If you allow empty blocks and you use it I hope that you would add a
comment so that the reader knows that you meant it.

for x in sequence: # this is an empty block

Is that better than "pass"?

-- 
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:darcy at Vex.Net
VoIP: sip:darcy at Vex.Net



More information about the Python-list mailing list