Why not allow empty code blocks?

Steven D'Aprano steve at pearwood.info
Fri Jul 29 23:35:48 EDT 2016


On Sat, 30 Jul 2016 06:19 am, BartC wrote:

> The language requires that blocks always contains 1 or more statements.
> Fair enough, except that 0 statements are often needed

They really aren't.

The standard library uses more "pass" statements than most code I've seen,
because of the large number of abstract methods and tests that use dummy
classes or methods. If you are writing library code, or a framework, with
lots of "do nothing" blocks that the caller is supposed to override, you
may find yourself doing the same. Even so, the number of "pass" statements
is a tiny proportion of code, less than one percent for Python 3.6:

[steve at ando Lib]$ wc -l *.py */*.py | tail -n 1
  541022 total
[steve at ando Lib]$ grep "pass$" *.py */*.py | wc -l
3286


Over two thirds of them are from the test suite:

[steve at ando Lib]$ grep "pass$" test/*.py | wc -l
2270

I feel confident in saying that if you find yourself writing "pass" in
application code (as opposed to writing a framework or unit tests for a
library) more than one time in a 500 lines of code, you're doing something
wrong. But even if it were as high as one time in 100 lines, it is still
not an onerous requirement.

You should see how many times Ruby programmers have to write "end", 99.9% of
which are unneeded but forced on them by the language.



-- 
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