PEP8 Python style guide Flashcards

To be able to write Pythonic code

You may prefer our related Brainscape-certified flashcards:
1
Q

What is PEP8?

A

It’s the style guide for writing Python code - how the code should look/be formatted to maximize readability

PEP stands for Python Enhancement Proposal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is PEP20?

A

The Zen of Python by Tim Peters.

A succinct list of 20 aphorisms about guiding principles of Python design.

import this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the recommendation indentation level

A

4 spaces per indentation level

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the preferred indentation method - tabs or spaces

A

Spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What should maximum line length be?

A

79 characters

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Should a line break before or after a Binary operator?

A

Before (follow Donald Knuth’s style) for new code e.g.

income = (gross_wages
          \+ taxable_interest
          \+ (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are rules for imports?

A

Imports should usually be on separate lines

import os
import sys

however, ok to do:

from subprocess import Popen, PIPE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How should imports be grouped?

A

Imports should be grouped in the following order:

Standard library imports.
Related third party imports.
Local application/library specific imports.
You should put a blank line between each group of imports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain block comments

A

Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

Paragraphs inside a block comment are separated by a line containing a single #.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain inline comments

A

Use inline comments sparingly.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Inline comments are unnecessary and in fact distracting if they state the obvious. Don’t do this:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain documentation strings … docstrings

A

Docstrings should be written for all public modules, functions and classes. They are of format:

”"”Return a foobang

Optional plotz says to frobnicate the bizbaz first.
“””

Can have single line docstrings as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Naming Conventions: Module
Modules are the actual files e.g. character_input.py
… may contain 0, 1, or more classes

A

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Naming Conventions: Package

Packages are essentially where code is structured in folders and sub-folders.

A

Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Naming Conventions: Class

A

Class names should normally use the CapWords convention.

CapitalizedWords (or CapWords, or CamelCase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Naming Conventions: Exception

A

Because exceptions should be classes, the class naming convention applies here. However, you should use the suffix “Error” on your exception names (if the exception actually is an error)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Naming Conventions: Functions and Variables

A

Function and Variables names should be lowercase, with words separated by underscores as necessary to improve readability.

17
Q

Naming Conventions: Function and Method arguments

A

Always use self for the first argument to instance methods.

Always use cls for the first argument to class methods.

If a function argument’s name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)

18
Q

Naming Conventions: Methods and Instance variables

A

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

Use one leading underscore only for non-public methods and instance variables.

To avoid name clashes with subclasses, use two leading underscores to invoke Python’s name mangling rules.

19
Q

Naming Conventions: Constants

A

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.

20
Q

What does it mean if code is not Pythonic?

A

Usually means that these lines of code do not follow the common guidelines and fail to express its intent in what is considered the best (hear: most readable) way.

21
Q

What is a programming idiom?

A

Put simply, it is a way to write code

22
Q

How do you assign to an variable you want to ignore?

A

Use the double underscore __ e.g below the __ holds the ‘.’ character

filename = ‘foobar.txt’
basename, __, ext = filename.rpartition(‘.’)

23
Q

How do you create a string from a list?

A

Use str.join() on an empty string e.g

letters = ['s', 'p', 'a', 'm']
word = ''.join(letters)
24
Q

How should you read from a file?

A

Use the with open syntax - this will automatically close files for you.

with open('file.txt') as f:
    for line in f:
        print line