Chapter 14Style
Code is read more often than it is written.
Despite this fact, programmers often write code as if they do not expect to have to maintain it or even read it in the future. This leads to code that is incomprehensible when it is read months or years later.
Therefore, one of the most important things you can do as a programmer (in any language) is to write readable code.
This chapter explores principles for writing readable code, as well as some of the standards adopted by the Python community at large for writing code in a consistent manner.
Principles
Before discussing specific standards that the Python community has adopted, or additional recommendations that have been proposed by others, it is important to consider a few overarching principles.
Remember that the purpose of readability standards is to improve readability. The rules exist to serve the people reading and writing code, not the other way around.
This section discusses a few principles to keep in mind.
Assume Your Code Will Require Maintenance
It is very easy to believe that the work you are doing at the moment will not require additions or maintenance in the future. This is because it is difficult to anticipate future needs, and it is easy to underestimate your own propensity to introduce bugs. However, very little of the code that you write will simply exist untouched into perpetuity.
If you assume that code that you are writing is going to be “a one-off” and something that you will not have to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access