Questions tagged [pep8]

Coding conventions and style guidelines for Python. Not to be confused with the PEP/8 assembly language.

PEP (Python Enhancement Proposals) 8 describes coding conventions for code in the standard Python library. This PEP covers how code should be commented, use tabs or spaces to indent code, naming convention, the use of non-semantic white space, etc.

Many large projects have adopted PEP8 (at least in part) as part of their style guides.

The tool pep8 will report code-conformance to the PEP8 guidelines.

Questions tagged as pep8 should relate to how to apply these guidelines to your code.

The full text of PEP8 can be found at python.org.

829 questions
142
votes
7 answers

PEP 8, why no spaces around '=' in keyword argument or a default parameter value?

Why does PEP 8 recommend not having spaces around = in a keyword argument or a default parameter value? Is this inconsistent with recommending spaces around every other occurrence of = in Python code? How is: func(1, 2,…
soulcheck
  • 36,297
  • 6
  • 91
  • 90
132
votes
6 answers

Tool to convert Python code to be PEP8 compliant

I know there are tools which validate whether your Python code is compliant with PEP8, for example there is both an online service and a python module. However, I cannot find a service or module which can convert my Python file to a self-contained,…
Chen Xie
  • 3,849
  • 8
  • 27
  • 46
124
votes
4 answers

Line continuation for list comprehensions or generator expressions in python

How are you supposed to break up a very long list comprehension? [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] I have also seen somewhere that people that dislike using '\' to break up…
sasker
  • 2,221
  • 2
  • 21
  • 26
119
votes
9 answers

PEP8 – import not at top of file with sys.path

Problem PEP8 has a rule about putting imports at the top of a file: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. However, in certain cases, I might want to…
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
117
votes
8 answers

How should I format a long url in a python comment and still be PEP8 compliant

In a block comment, I want to reference a URL that is over 80 characters long. What is the preferred convention for displaying this URL? I know bit.ly is an option, but the URL itself is descriptive. Shortening it and then having a nested comment…
Zach
  • 18,594
  • 18
  • 59
  • 68
112
votes
4 answers

python pep8 class in init imported but not used

I'm doing PEP8 checks in python using the python flake8 library. I have an import statement in an __init__.py file in one of my sub-modules which looks like this: from .my_class import MyClass The reason I have this line in the init file is so that…
Salvius
  • 1,193
  • 2
  • 8
  • 8
112
votes
7 answers

Define functions with too many arguments to abide by PEP8 standard

I have defined a function with a long list of arguments. The total characters in definition is above 80 and doesn't abide by PEP8. def my_function(argument_one, argument_two, argument_three, argument_four, argument_five): What can be the best…
Sudip Kafle
  • 4,286
  • 5
  • 36
  • 49
107
votes
7 answers

Should I always specify an exception type in `except` statements?

When using PyCharm IDE the use of except: without an exception type triggers a reminder from the IDE that this exception clause is Too broad. Should I be ignoring this advice? Or is it Pythonic to always specific the exception type?
HorseloverFat
  • 3,290
  • 5
  • 22
  • 32
104
votes
8 answers

How to disable a pep8 error in a specific file?

I tried with #:PEP8 -E223 or # pep8: disable=E223 I thought the second would work but doesn't seems to work. Do you have an idea how I can handle this ?
Flows
  • 3,675
  • 3
  • 28
  • 52
92
votes
8 answers

How to prevent "too broad exception" in this case?

I have a list of functions that may fail and, if one fails, I don't want the script to stop, but to continue with next function. I am executing it with something like this : list_of_functions = [f_a, f_b, f_c] for current_function in…
Blusky
  • 3,470
  • 1
  • 19
  • 35
92
votes
6 answers

Import order coding standard

PEP8 suggests that: 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. Is there a way to…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
90
votes
6 answers

How to integrate pep8.py in Eclipse?

A little background: PEP 8 is the Style Guide for Python Code. It contains the conventions all python programmers should follow. pep8.py is a (very useful) script that checks the code formating of a given python script, according to PEP 8. Eclipse…
David Arcos
  • 5,957
  • 5
  • 30
  • 39
90
votes
13 answers

Python: using 4 spaces for indentation. Why?

While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend to have 4 spaces, but historically for me it's unusual. So, can anyone convince me to use 4 spaces instead of 2? What pros and cons? P.S. And finally, what's easy way…
HardQuestions
  • 4,075
  • 7
  • 34
  • 39
83
votes
3 answers

Python PEP8: Blank lines convention

I am interested in knowing what is the Python convention for newlines between the program parts? For example, consider this: import os def func1(): def func2(): What should be the ideal newline separation between: The import modules and…
user225312
  • 126,773
  • 69
  • 172
  • 181
83
votes
4 answers

what is trailing whitespace and how can I handle this?

I have some code like: if self.tagname and self.tagname2 in list1: try: question = soup.find("div", "post-text") title = soup.find("a", "question-hyperlink") …
Amy Obrian
  • 1,183
  • 2
  • 11
  • 19
1
2
3
55 56