A Python style guide that doesn't make you think.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
HISTORY.md Add version number Apr 25, 2018
README.md Bump version to 1.1.0 May 9, 2018

README.md

Mitch's Unofficial Python Style Guide

A Python style guide that doesn't make you think.

Background

You can read all about it in my blog post.

Rules

Readable consistency

Style guides are meant to enforce consistency and readability in your code base. But if being consistent sacrifices readability, then you’re better off being inconsistent.

Alas, this introduces some ambiguity. So when in doubt, follow the style guide. (Or not. It’s up to you.)

Follow PEP 8

Read it. Live it. Love it.

Almost always use single quotes

Use single quotes unless the string itself contains single quotes. In which case, use double quotes.

Yes:

error_message = 'You have done something wrong'
consolation_message = "It's okay. People make mistakes."
scientific_names['pineapple'] = 'Ananus sativus'

No:

error_message = "You have done something wrong"
consolation_message = 'It\'s okay. People make mistakes'
scientific_names["pineapple"] = "Ananus sativus"

For triple-quoted strings however, always use double quotes, c/o PEP 8 String Quotes.

Note: If double quotes is the default quote for your keyboard locale, use double quotes instead.

Line breaks and trailing commas

Break lists, sets, and dictionaries into multiple lines. The first item should be on the next line. Add a trailing comma.

Yes:

fruits = [
    'pineapple',
    'guava',
    'banana',
    'tomato',
]

scientific_names = {
    'pineapple': 'Ananus sativus',
    'guava': 'Psidium guava',
    'banana': 'Musa paradisicum',
    'tomato': 'Lycopersican esculentum',
}

No:

fruits = ['pineapple',
          'guava',
          'banana',
          'tomato']

scientific_names = {'pineapple': 'Ananus sativus',
                    'guava': 'Psidium guava',
                    'banana': 'Musa paradisicum',
                    'tomato': 'Lycopersican esculentum'}

If a list does not exceed the max line length, then it could be written in one line.

OK:

fruits = ['pineapple', 'guava', 'banana', 'tomato']

Long function calls

Break function calls into multiple lines if they exceed the max line length. Similar to lists.

Yes:

guess_fruit(
    color='red',
    size='small',
    climate='cold',
    region='north america',
    minimum_tastiness_factor=4.3,
)

No:

guess_fruit(color='red', size='small', climate='cold',
            region='north america', minimum_tastiness_factor=4.3)

If the arguments do not exceed the max line length, then you may keep the arguments in one line. No trailing comma. The closing parenthesis must be on the same level as the opening parenthesis.

OK:

guess_fruit(
    color='red', size='small', climate='cold', region='north america'
)

No:

guess_fruit(
    color='red', size='small', climate='cold', region='north america')

guess_fruit(
    color='red', size='small', climate='cold', region='north america',
)  # No trailing comma

Long if statements

Break long if statements similar to long function calls. Break before operators.

Yes:

if (
    fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
    and fruit.region == 'north america'
):
    pass

if (
    fruit.color == 'red' and fruit.size == 'small' and fruit.climate == 'cold'
):
    pass

No:

if (fruit.color == 'red'
        and fruit.size == 'small'
        and fruit.climate == 'cold'
        and fruit.region == 'north america'):
    pass

if (
    fruit.color == 'red' and fruit.size == 'small' and fruit.climate == 'cold'
    and fruit.region == 'north america'
):
    pass

Long comprehensions

Break long list, set, and dictionary comprehensions into multiple lines. The item first, then the for statements, then the if statements.

Yes:

best_fruits_names = [
    fruit.name
    for fruit in fruits
    if fruit.color == 'red'
    and fruit.size == 'small'
    and fruit.climate == 'cold'
]

1.1.0 (2018-05-09)