Why not allow empty code blocks?

BartC bc at freeuk.com
Sat Jul 23 10:06:41 EDT 2016


On 23/07/2016 14:54, D'Arcy J.M. Cain wrote:
> On Sat, 23 Jul 2016 16:13:58 +0300
> Marko Rauhamaa <marko at pacujo.net> wrote:
>> "D'Arcy J.M. Cain" <darcy at Vex.Net>:
>>> 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.
>>
>> Just let the syntax speak for itself. The code means what the language
>> definition says it means.
>
> Exactly so given this...
>
> for x in sequence:
> print("Something")
>
> If the language allowed that then we know exactly what the programmer
> said.  What we don't know is what the programmer meant.  The above is a
> simple and obvious statement but real code is more complicated.  Add a
> few comments and a little nesting and what used to be obvious isn't.
> The interpreter would still know what the programmer said but without
> the pass requirement it is easier for it to be not what he meant.
>
> Python has some nice features like this one that protect the programmer
> and allow much faster development because there is a safety net.  If
> you like knives without handles you know where to find Perl.

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?

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:

  for x in sequence:
      print("Something")
  end
  print("Something else")

(And no speculation at all if 'end' was mandatory. Python already 
provides 'else' (and 'except'?) which can do a similar job in some 
circumstances.)

-- 
Bartc



More information about the Python-list mailing list