Skip to content

Instantly share code, notes, and snippets.

@cham11ng
Last active July 7, 2017 19:43
Show Gist options
  • Save cham11ng/3981fee8fab6e3f4a2efa8133d0d14f4 to your computer and use it in GitHub Desktop.
Save cham11ng/3981fee8fab6e3f4a2efa8133d0d14f4 to your computer and use it in GitHub Desktop.
Python Notes

Python

  • General-Purpose Programming Language
  • Python Software Foundation
  • Creator Guido van Rossum (Dutch Programmer, Netherlands)
  • Logo, two snakes

PyPI

The Python Package Index is a repository of software for the Python programming language. The Python Package Index (PyPI) hosts thousands of third-party modules for Python.

PEP 8

Python Enhancement Proposal

Source File Encoding

Code in the core Python distribution should always use UTF-8 (or ASCII in Python 2).

Code lay-out

  • Indentation Use 4 spaces per indentation level.

NO

# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)

# Further indentation required as indentation is not distinguishable.
def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

YES

# 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)

The closing brace/bracket/parenthesis on multi-line constructs.

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

Maximum Line Length

Limit all lines to a maximum of 79 characters.

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

Should a line break before or after a binary operator?

NO

# No: operators sit far away from their operands
income = (gross_wages +
          taxable_interest +
          (dividends - qualified_dividends) -
          ira_deduction -
          student_loan_interest)

YES

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

Imports

Imports should usually be on separate lines, e.g.:

NO

import sys, os

YES

import os
import sys

It's okay to say this though:

from subprocess import Popen, PIPE

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.

Module level dunder names

Module level "dunders" (i.e. names with two leading and two trailing underscores) such as all , author , version , etc. should be placed after the module docstring but before any import statements except from future imports.

"""
This is the example module.
This module does stuff.
"""

from __future__ import barry_as_FLUFL

__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'

import os
import sys

String Quotes

In Python, single-quoted strings and double-quoted strings are the same. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.

For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in (PEP 257)[https://www.python.org/dev/peps/pep-0257].

Whitespace in Expressions and Statements

Yes: if x == 4: print x, y; x, y = y, x
No:  if x == 4 : print x , y ; x , y = y , x

NO

i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)

Yes

i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

NO

def complex(real, imag = 0.0):
    return magic(r = real, i = imag)

YES

def complex(real, imag=0.0):
    return magic(r=real, i=imag)

Prescriptive: Naming Conventions

  • Function, Package and Module Names
    • Functions, Packages and Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability although use of underscores is discouraged.
  • Class Names
    • Class names should normally use the CapWords convention.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment