PEP 8 style suggestions

Last but definitely not least, PyCharm will inform you of any improvements that can be made in your code with regard to the official style guide for writing Python code, PEP 8.

For more information about this style guide, head on over to www.python.org/dev/peps/pep-0008/.

We have already seen an example of using PyCharm to follow PEP 8 conventions with the wrapping limit set to 79 in each line of the editor.

Now, hover over the beginning of the main scope in our current program, if __name__ == '__main__':. You will see a message saying PEP 8: expected 2 blank lines after class of function definition, found 1. As suggested, following PEP 8, programmers should have two blank lines after the declaration of a class or, in our case, a function before writing new code blocks.

To fix this problem, simply add another blank line after the main() function, and the warning will go away.

As you can see, this feature is quite convenient for Python programmers. No one really wants to write bad code that does not follow the official style guide, but not many people really remember all the instances and subtleties of PEP 8 off by heart. Having code inspections offering suggestions so that we can format our Python code in PyCharm effectively takes care of this problem for us.

If, for some reason, you are not seeing the preceding warnings, open PyCharm's settings and navigate to Editor | Inspections. From the list included in the main section, expand the Python item and ensure that the two sub-items, that is, PEP 8 coding style violation and PEP 8 naming convention violationare enabled, as shown in the following screenshot:

Enabling PEP 8 conventions in PyCharm

After successfully addressing each of these problems, there should be no warnings or error in your current code now, and hopefully the icon on the top-right corner of your editor window is a green checkmark. Again, since we already have a great IDE to detect all potential problems in our code, the least we can do is leverage that and strive for better code quality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.227.194