Title: Theory / Unimportant / CS / Programming / Python / PEP / 8 Style guide/ / Style
On every note of this deck, unless a note says otherwise:
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding

Question: Number of space by indentation level
Answer: 4
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Indentation
Context: pep8

Question: Continuation lines should align wrapped elements
Answer: either vertically using Python's implicit line joining inside parentheses, brackets and braces
foo = long_function_name(var_one, var_two,
var_three, var_four)
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Indentation
Context: pep8
Answer2: or using a hanging indent
def long_function_name(
var_one, var_two,
var_three, var_four):
print(var_one)

Question: Why indentation after a «if (» on multiline should be considered ?
Answer: because «if (» is 4 char long. Thus the next line, aligned after the parenthesis, may look line the body of the if
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Indentation
Context: pep8

Question: Where to put the brace/bracket/parenthesis on multiline constructs
Answer: Either line up under the first non-whitespace character of the last line of list, as in:
my_list = [
1, 2, 3,
4, 5, 6,
]
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Indentation
Context: pep8
Answer2: or it may be lined up under the first character of the line that starts the multiline construct, as in: my_list = [
1, 2, 3,
4, 5, 6,
]

Question: Tab or spaces
Answer: Spaces
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Tabs or Spaces?
Context: pep8

Question: Lines have length
Answer: 79 chars
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Maximum Line Length
Context: pep8
Answer2: 72 for long block of texts

Question: How to break long lines
Answer: by wrapping expressions in parentheses
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Maximum Line Length
Context: pep8
Answer2: using \ at the end of the line, if nothing else is possible

Question: Break after or before binary operators, and why ?
Answer: before. So Binary operators are aligned
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Should a Line Break Before or After a Binary Operator?
Context: pep8

Question: Surround top-level function and class definitions with
Answer: two blank lines.
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Blank Lines
Context: pep8

Question: Method definitions inside a class are surrounded by
Answer: a single blank line.
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Blank Lines
Context: pep8

Question: What file encoding to state
Answer: only the non-standard one
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Source File Encoding
Context: pep8

Question: What file encoding to use
Answer: UTF-8 for python 3
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Source File Encoding
Context: Python
Answer2: Ascii for Python2

Question: How to do multiple import
Answer: one source by line
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Imports
Context: pep8

Question: In which order is the preamble
Answer: module comments
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Imports
Context: pep8
Answer2: docstring
Answer3: imports
Answer4: module globals
Answer5: module constants
Extra: Encoding ?

Question: In which order are the import made
Answer: future
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Imports
Context: Pep8
Answer2: Dunder
Answer3: standard library
Answer4: related third party
Answer5: local

Question: Which kind of import is preferred
Answer: absolute import
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Imports
Context: pep8
Answer2: relative import if the default would be unnecessarily verbose

Question: When «from foo import *» is acceptable
Answer: to create a module modifying an API
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Code Lay-out
Section: Imports
Context: pep8

Question: Which style of quote to use
Answer: be consistent
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Code Lay-out
Section: String Quotes
Context: Python
Answer2: if your string contain one kind of quote, use the other

Question: Number of space for binary operators
Answer: same number on both side
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8
Answer2: at most one
Answer3: 0 for operator of lowest priorities

Question: Number of space after a colon
Answer: 1 in general
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8
Answer2: 0 if the colon is followed by a closing delimiter

Question: Number of space before a colon
Answer: 0
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space after a semicolon
Answer: 1
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: Pep8

Question: Number of space before a ":"
Answer: 0 in general.
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8
Answer2: 1 for slices
Answer3: 0 if a slice as an empty part, of the empty part

Question: Number of space before a open parenthesis (
Answer: 0
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space before a open parenthesis [
Answer: 0
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space before an assigment =
Answer: 1
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: Python

Question: Number of space after an assigment =
Answer: 1
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space ending a line
Answer: none
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space before a key-word argument =
Answer: 0 in general
1 if there is an anotation
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space after a key-word argument =
Answer: 0 in general
1 if there is an anotation
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space before ->
Answer: 1
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: Number of space after ->
Answer: 1
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: when can the body of a if/for/while be on the same line as the if/for/while
Answer: if the line is is short, and contains a single statement without (
)
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Whitespace in Expressions and Statements
Section: Pet Peeves
Context: pep8

Question: How to create a one-element tuple
Answer: (foo,)
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: When to Use Trailing Commas
Context: pep8

Question: When to use trailing commas
Answer: imagine that you later add element to the list. If adding a comma would make you change a line, and thus increase the number of line changed in a VCS, then put the trailing comma
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: When to Use Trailing Commas
Context: pep8

Question: Indentation of block comment
Answer: same as the code it comments
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Comments
Section: Block Comments
Context: pep8

Question: Number of space after a #
Answer: one. Except for indentation inside the comment
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Comments
Section: Block Comments
Context: pep8

Question: How to separate paragraph in a comment
Answer: with a line containing a single #
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Comments
Section: Block Comments
Context: pep8

Question: What rule comments should follow
Answer: respect case of identifier, even if it breaks english standard
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Comments
Section: Block Comments
Context: pep8
Answer2: comment should be consistend with the code or removed
Answer3: comment should be in english unless the code is never shared outside from people speaking its language

Question: Rule for inline comment
Answer: avoid them. Unless they really add some information
Page: https://www.python.org/dev/peps/pep-0008/#source-file-encoding
Chapter: Comments
Section: Block Comments
Context: pep8

Name: upper case
Definition: FOOBAR
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable

Name: upper case with underscores
Definition: FOO_BAR
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable

Name: lower case with underscores
Definition: foo_bar
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable

Name: CapitalizeWords
Definition: FooBar (acronym remain capitalized everywhere)
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable
Name2: CamelCase
Abbreviation: CapWords

Name: mixed case
Definition: fooBar
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable

Name: Capitalized words with underscore
Definition: Foo_Bar
Chapter: Naming Conventions
Section: Descriptive: Naming Styles
Context: naming style of variable

Question: Which variable name to avoid
Answer: I, O, l
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Names to Avoid
Context: pep8

Question: Name of modules should be
Answer: all-lowercase names
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Names to Avoid
Context: pep8
Answer2: Underscores can be used in the module name if it improves readability

Question: Which case convention for class names
Answer: CapWords
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Class Names
Context: pep8
Answer2: function convention is the use of the class is mainly a callable

Question: What convention to use for variable
Answer: CapWords
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Type Variable Names
Context: pep8
Answer2: short name

Question: What convention to use for exceptions
Answer: class convention
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8
Answer2: prefix Error for erros

Question: What convention for global variable names
Answer: function convention
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8
Answer2: use __all__ mechanism for variables to export from the module

Question: What convention for function name
Answer: lowercase, words separated by underscore
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8

Question: What convention for variable name
Answer: lowercase, words separated by underscore
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8

Question: What convention for the first argument of instance method
Answer: call it «self»
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8

Question: What convention for the first argument of class method
Answer: call it «cls»
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8

Question: What convention for a variable whose name clash with a reserved keyword
Answer: add a trailing _
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Exception Names
Context: pep8

Question: What convention for method names/instance variables
Answer: function naming rules
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Method Names and Instance Variables
Context: pep8
Answer2: start by a _ for non-public method
Answer3: two __ to avoid clashes in underlying method

Question: What convention for constants
Answer: upper case with _ separating words
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Method Names and Instance Variables
Context: pep8

Question: In doubt for whether attribute should be public or not
Answer: choose non-public
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: How to distinguish public and non-public attributes
Answer: _ prefix iff non-public
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8
Answer2: list public attributes in __all__

Question: How to compare singleton
Answer: with «is» and «is not»
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: What to do when an implementation is unusually efficient in a python implementation, and not anywhere else
Answer: avoid it, to have more similar effect everytime
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Which operation is used to compute sort
Answer: <
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Which operation is used to compute min
Answer: <
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Which operation is used to compute max
Answer: >
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Which ordering operation should you implement
Answer: all 6 or non. (You may use functools.total_ordering() decorator)
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Why to avoid "f = lambda x: 2*x"
Answer: The lambda has no name, thus can't be used for recursion and is hard to debug
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Difference between BaseException and Exception
Answer: Exceptions inheriting directly Base should usually not be catched
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: How to do exception chaining
Answer: raise foo from bar
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: How to catch exception, but only the one which should be catched
Answer: except Exception (otherwise, you'll catch Control-C, and systemExit)
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: rule for try/except
Answer: put minimal number of things in the try (i.e. assign every result of functions to variable, instead of using them in other construct)
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: rule for variables useless in the end of a section of code
Answer: use a with statement for this variable
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: rule for return
Answer: either every thing return or value, or nothing
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: Which string function should be applied
Answer: methods and not module
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: how to check for suffix/prefix of string
Answer: .startwith(foo) or .endwith(foo) instead of splicing
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8

Question: how to check a type
Answer: isinstance, and not type (foo) is
Page: https://www.python.org/dev/peps/pep-0008/
Chapter: Prescriptive: Naming Conventions
Section: Designing for Inheritance
Context: pep8