Once i came across a problem in Hacker Rank, i solved it. But when i had to revisit the problem, i was not able to read my code. The way the code was written made it very unpleasant to read and understand. Then, I had a spark to better enhance my code. And when i came across PEP-8 ( Python Enhancement Proposal) i thought it is a better enhancement available and followed widely.
This article will be about discussion on PEP - 8. And this is a summary of the documentation available at https://www.python.org/dev/peps/pep-0008/
Indentation:
- Use 4 spaces per Indentation level.
- In case a long line has to be continued in the next line, the continuation line should align wrapped elements either vertically using python's implicit line joining inside parenthesis or use hanging indent.
- In hanging indent, there should be no argument in the first line and further indentation has to be used to distinguish it from the next line.
- We have two options aligned with the wrapped element or the hanging indent, the former is preferred in case of 'if' statement. Since "if (" by default has 4 characters ( 'if' has two letters, a white space and a opening bracket).
- The closing brace/ bracket/ parenthesis on multi line constructs either line up under the first non-white space character of the last line or it may be lined up under the first line of the multi line construct.
- Its better to break a binary operator for better readability of code.
Maximum Line Length:
- Limit all the lines to a maximum of 79 characters. It is fine to have up to 99 characters of code in the line if it is not widely shared or documented.
- For flowing long blocks of text with fewer structural restriction ( doc string or comments), the line length should be limited to 72 characters.
- '\' can be used to continue a line.
This article will be about discussion on PEP - 8. And this is a summary of the documentation available at https://www.python.org/dev/peps/pep-0008/
Indentation:
- Use 4 spaces per Indentation level.
- In case a long line has to be continued in the next line, the continuation line should align wrapped elements either vertically using python's implicit line joining inside parenthesis or use hanging indent.
- In hanging indent, there should be no argument in the first line and further indentation has to be used to distinguish it from the next line.
- We have two options aligned with the wrapped element or the hanging indent, the former is preferred in case of 'if' statement. Since "if (" by default has 4 characters ( 'if' has two letters, a white space and a opening bracket).
- The closing brace/ bracket/ parenthesis on multi line constructs either line up under the first non-white space character of the last line or it may be lined up under the first line of the multi line construct.
- Its better to break a binary operator for better readability of code.
Maximum Line Length:
- Limit all the lines to a maximum of 79 characters. It is fine to have up to 99 characters of code in the line if it is not widely shared or documented.
- For flowing long blocks of text with fewer structural restriction ( doc string or comments), the line length should be limited to 72 characters.
- '\' can be used to continue a line.
Comments
Post a Comment