Learning Python: Python Styling Guide

Python has styling conventions. These styling conventions are defined in the Python Enhancement Proposals (PEP8). Here are the condensed version of the rules:

  • Indentation should be done using spaces and not tabs.

  • Indentation is done using 4 spaces.

  • Python files are UTF-8 encoded.

  • 80 columns is the maximum for code per line.

  • Each statement is to be written on a line of its own.

  • Variable names, function names, and filenames are all written in lowercase. Underscores are used when separating words (aka snake_case).

  • The names of classes are capitalized. If the name of a class is more than one word, use CamelCase.

  • The names of packages are written in lowercase and no underscore is needed at all.

  • Variables that are constants are written in uppercase.

  • Make sure that variables have names that have meaning (e.g. get_user).

  • Leave a space when dealing with operators (e.g. a + b).

  • Whitespace that is unnecessary should be removed.

  • Leave a blank line before writing a function.

  • When writing methods in a class leave a blank line between them.

  • Make sure to use blank lines inside functions and methods in order to keep related blocks of code separate and make your code more readable.

This blog post was originally published on my blog Communicode, where I write about different tech topics.

Python
Avatar for Muhammad Asfour

Written by Muhammad Asfour

I am a Full Stack Developer with over 5 years of experience. I have worked with different tech stacks such as: Groovy/Java, PHP, .NET/.NET Core (C#), Node.js, React, Angular, and Vue.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.