Monday, August 3, 2015

Python PEP 8

"The single most important formatting convention that you can follow is to indent your programs properly, so the indentation conveys the structure of the program to the reader at a glance. Indentation must be done carefully, however, lest you confuse rather than enlighten." -- The Elements of Programming Style

The PEP-8 guidelines were created to improve communication between programmers. Properly formatted source code is easier to understand for the same reason that books are easier to read if they are formatted into sentences and paragraphs. 

Here is the PEP8 document. Read this first.


PyLint
PyLint checks your code to see if it complies with the PEP8 recommendations. It can also help you refactor your program by detecting duplicate code. PyLint is integrated with various IDE's and editors. You can view the complete list here.

You can configure PyLint if you don't like the default settings. Follow the instructions in the PyLint tutorial.


Online Tools
This online PyLint checker lets you can upload your files and check them.


Videos
Beyond PEP 8 -- Best practices for beautiful intelligible code - PyCon 2015
Raymond Hettinger is always a popular speaker. Learn to avoid some of the hazards of the PEP 8 style guide and learn what really matters for creating beautiful intelligible code.


Formatters
AutoPEP8 will reformat your source code so it complies with the PEP8 guidelines. As always, you should make a backup copy of your files (or put them under version control) before letting any tool modify them. You can download it here.


Google
Google has developed their own Python style guide.


More Information
The web is full of Python style guides. Doing a Google search for [python coding style] will find them.

No comments:

Post a Comment