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
33
votes
1 answer

Why are unittest2 methods camelCase if names_with_underscores are preferred?

Here's the section of PEP8 that describes how function names should be: Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the…
the_drow
  • 18,571
  • 25
  • 126
  • 193
31
votes
2 answers

Using shorter textwidth in comments and docstrings

From the mighty PEP 8: [P]lease limit all lines to a maximum of 79 characters. For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended. When editing Python code in Vim, I set my textwidth to…
Eric Naeseth
  • 2,293
  • 2
  • 19
  • 18
31
votes
4 answers

Triple-double quote v.s. Double quote

What is the preferred way to write Python doc string? """ or " In the book Dive Into Python, the author provides the following example: def buildConnectionString(params): """Build a connection string from a dictionary of parameters. …
Mingyu
  • 31,751
  • 14
  • 55
  • 60
31
votes
1 answer

How to fix: W602 deprecated form of raising exception

If I use pylint (via sublimerlinter) I get following warning message: W602 deprecated form of raising exception This I how I use exceptions in my code: if CONDITION == True: raise ValueError, HELPING_EXPLANATION
Framester
  • 33,341
  • 51
  • 130
  • 192
29
votes
6 answers

Line is too long. Django PEP8

PEP8 info: models.py:10:80: E501 line too long (83 > 79 characters) Models.py: field = TreeForeignKey('self', null=True, blank=True, related_name='abcdefgh') How to correctly write this line?
Angelina
  • 363
  • 1
  • 5
  • 9
28
votes
3 answers

How do I enable auto code formatting for flake8 in PyCharm

I use Tox to run unit tests, with a flake8 command that checks for code formatting errors. Each time I code in PyCharm, I run tox then realise I have a bunch of annoying formatting errors I have to back and manually fix. I would like PyCharm to…
Ryu S.
  • 1,538
  • 2
  • 22
  • 41
28
votes
4 answers

Python: PEP 8 class name as variable

Which is the convention according to PEP 8 for writing variables that identify class names (not instances)? That is, given two classes, A and B, which of the following statements would be the right one? target_class = A if some_condition else…
LostMyGlasses
  • 3,074
  • 20
  • 28
28
votes
2 answers

How do I break a link in a rst docstring to satisfy pep8?

I'm using Sphinxdoc to generate api documentation, and I've run into a problem with pep8 conformance when writing a docstring. As you can see below, the link to the OWASP site ends at column 105, far past what pep8 dictates maximum-line-length def…
thebjorn
  • 26,297
  • 11
  • 96
  • 138
26
votes
3 answers

How to write a pep8 configuration (pep8.rc) file?

I found the documentation for pep8 but wasn't able to understand how to write these. I couldn't even find any examples with options other than setting max-line-length and ignore. I am trying to write a .pep8.rc file in which, among other things, I…
Aditya Srivastava
  • 417
  • 1
  • 6
  • 12
25
votes
1 answer

Getting PEP8 "invalid escape sequence" warning trying to escape parentheses in a regex

I am trying to escape a string such as this: string = re.split(")(", other_string) Because not escaping those parentheses gives me an error. But if I do this: string = re.split("\)\(", other_string) I get a warning from PEP8 that it's an invalid…
Jack Avante
  • 1,405
  • 1
  • 15
  • 32
24
votes
5 answers

ipython3 notebook vertical margin/marker line at 80 characters

How to make ipython3 notebook show a vertical margin/marker line at 80 characters ? How to get i-bar location in ipython3 notebook ? (e.g. line 30 character 56) These features assist in writing codes complying with PEP8. These features are…
Neeraj Hanumante
  • 1,575
  • 2
  • 18
  • 37
24
votes
2 answers

Idiom for long tuple unpacking

Scenario: you have a long tuple as a result of a SQL query and want to unpack it into individual values. What's the best way to do that while conforming to PEP8? So far I have these three options: single assignment, use backslash to split to…
koniiiik
  • 4,240
  • 1
  • 23
  • 28
24
votes
6 answers

Is De Morgan's Law Pythonic?

Which of the following if statements is more Pythonic? if not a and not b: do_something OR if not ( a or b ): do something Its not predicate logic so I should use the Python key words because its more readable right? In the later solution…
nialloc
  • 805
  • 7
  • 17
23
votes
5 answers

Complete code example that demonstrates all PEP-8 rules

I want my code to be PEP-8 compliant. However, reading the PEP8-page everytime I forgot any of the rules is time-consuming. Much faster would be if I had a code example, which demonstrates all PEP-8 rules. Is there any code example out there which…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
23
votes
1 answer

How to break long string lines for PEP8 compliance?

I have many long lines like this in the project and don't know how to break it to keep PEP8 happy. PEP8 shows warning from .format(me['id']) pic_url = "http://graph.facebook.com/{0}/picture?width=100&height=100".format(me['id']) How can I break the…
Houman
  • 64,245
  • 87
  • 278
  • 460