Writing Beautiful Python

An overview of pep8

About Me

  • Luke Spademan
  • Self-taught pythonista
  • Programming in python for ~5 years
  • I've had to read and work on badly written code

What is Beautiful?

“Pleasing the senses or mind aesthetically.” -- Oxford Dictionary

What is Beautiful code?

  • 'looks nice'
  • easy to read
  • consistant formatting

Style Guides

  • define how code should be formatted within a project
  • most projests have them
  • some languages have one

But why?

PEP8

PEP what?

PEP = Python Enhancement Proposals
PEP8 = 8th PEP

Consistancy

“A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.” -- PEP8

Tabs vs Spaces

  • 4 spaces should be used for indentation
  • spaces and tabs should never be mixed
  • tabs should only be used to be consistant with pre exisiting code

Indentation

Good Examples


# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)
					

# Add 4 spaces (an extra level of indentation)
# to distinguish arguments 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)
					

What not to do


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

Line Length

  • Lines no longer than 79 characters
  • Docstrings, comments, etc. should be limited to 72
“The limits are chosen to avoid wrapping in editors with the window width set to 80.” -- PEP8

All these rules...

Linters

Linters

  • check for formatting issues / pep8 violations
  • shout at you
  • put them in your ci
  • pylint
  • flake8

def foo(bar):
    print("hello "+bar)

def baz():
    print("Testing 1 2 1 2")

foo()
					

************* Module example
example.py:1:0: C0111: Missing module docstring (missing-docstring)
example.py:1:0: C0102: Black listed name "foo" (blacklisted-name)
example.py:1:0: C0102: Black listed name "bar" (blacklisted-name)
example.py:1:0: C0111: Missing function docstring (missing-docstring)
example.py:4:0: C0102: Black listed name "baz" (blacklisted-name)
example.py:4:0: C0111: Missing function docstring (missing-docstring)
example.py:7:0: E1120: No value for argument 'bar' in function call (no-value-for-parameter)

					

Formatters

Black

The Uncompromising Code Formatter

  • Formats your code, so you don't have to
  • Still in beta
  • Not perfect
  • Does most things
  • Editor intergration

def foo(bar):
    print("hello " + bar)


def baz():
    print("Testing 1 2 1 2")


foo()