Python style guide Flashcards

1
Q

indentation alligment of ( ), [ ], { }

A
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)
# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)
# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

indentation for if statements when statement leaves no room for parenthesis

A

No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()

Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can frobnicate.
do_something()

Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
and that_is_another_thing):
do_something()

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

indentation: The closing brace/bracket/parenthesis on multi-line constructs may either line up under the first non-whitespace character of the last line of list.

A
my_list = [
    1, 2, 3,
    4, 5, 6,
    ]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
    )

or

my_list = [
    1, 2, 3,
    4, 5, 6,
]
result = some_function_that_takes_arguments(
    'a', 'b', 'c',
    'd', 'e', 'f',
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

maximum line length

A

Limit all lines to a maximum of 79 characters.

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

Blank Lines

A

Separate top-level function and class definitions with two blank lines.

Method definitions inside a class are separated by a single blank line.

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

Imports

A

Imports should be on separate lines

Yes: import os
import sys

No: import sys, os

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

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
7
Q

Imports- absolutes

A

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured.

import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

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

explicit relative imports

A

explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:

from . import sibling
from .sibling import example

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

importing classes

A

When importing a class from a class-containing module, it’s usually okay to spell this:

from myclass import MyClass
from foo.bar.yourclass import YourClass

If this spelling causes local name clashes, then spell them

import myclass
import foo.bar.yourclass

and use “myclass.MyClass” and “foo.bar.yourclass.YourClass”.

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

whitespace

A

Avoid extraneous whitespace in the following situations:

Immediately inside parentheses, brackets or braces.
Immediately before a comma, semicolon, or colon
Immediately before the open parenthesis that starts the argument list of a function call
Immediately before the open parenthesis that starts an indexing or slicing
More than one space around an assignment (or other) operator to align it with another.

always surround binary operators with single space.
operators style
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

comments

A

You should use two spaces after a sentence-ending period.

PEP 257 describes good docstring conventions. Note that most importantly, the “”” that ends a multiline docstring should be on a line by itself
“"”Return a foobang

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

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

indentation (basic)

A

4 spaces (try not to use tab)

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