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
79
votes
2 answers

pep8 warning on regex string in Python, Eclipse

Why is pep8 complaining on the next string in the code? import re re.compile("\d{3}") The warning I receive: ID:W1401 Anomalous backslash in string: '\d'. String constant might be missing an r prefix. Can you explain what is the meaning of the…
alandarev
  • 8,349
  • 2
  • 34
  • 43
78
votes
3 answers

Correct style for line breaks when chaining methods in Python

I have some code like this. Should the break occur before the periods or after? # before my_var = somethinglikethis.where(we=do_things).where(we=domore).where(we=everdomore) # this way my_var = somethinglikethis.where(we=do_things) \ …
JiminyCricket
  • 7,050
  • 7
  • 42
  • 59
68
votes
1 answer

Python function argument list formatting

What is the best way to format following piece of code accordingly to PEP8: oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=ACCESS_TOKEN_URL) The problem is that if I place more than…
sam
  • 1,211
  • 2
  • 13
  • 15
68
votes
5 answers

How to format a python assert statement that complies with PEP8?

How does one format a long assert statement that complies with PEP8? Please ignore the contrived nature of my example. def afunc(some_param_name): assert isinstance(some_param_name, SomeClassName), 'some_param_name must be an instance of…
stantonk
  • 1,922
  • 1
  • 18
  • 24
68
votes
4 answers

How to properly use python's isinstance() to check if a variable is a number?

I found some old Python code that was doing something like: if type(var) is type(1): ... As expected, pep8 complains about this recommending usage of isinstance(). Now, the problem is that the numbers module was added in Python 2.6 and I need to…
sorin
  • 161,544
  • 178
  • 535
  • 806
60
votes
4 answers

How can I make my Python code stay under 80 characters a line?

I have written some Python in which some lines exceed 80 characters in length, which is a threshold I need to stay under. How can I adapt my code to reduce line lengths?
sahana
  • 609
  • 1
  • 5
  • 3
59
votes
1 answer

How come the Python's logging module doesn't follow PEP8 conventions?

This is is just a curiosity with historical purposes: I was wondering if someone knows why the very widely used (and core module) logging doesn't follow the Python's PEP-8 naming convention. For instance, in >>> import logging >>> log =…
Savir
  • 17,568
  • 15
  • 82
  • 136
58
votes
3 answers

Code formatter like nb_black for google colab

I know that for jupyter notebooks and jupyter lab, there are available code formatter extensions such as nb_blackor blackcellmagic. However when I installed them, it doesn't seem to work on google colab. Do you know if there are any native option in…
Syndo rik
  • 755
  • 1
  • 7
  • 15
57
votes
3 answers

Why does PyCharm use 120 Character Lines even though PEP8 Specifies 79?

PEP8 clearly specifies 79 characters, however, PyCharm defaults to 120 and gives me the warning "PEP8: line too long (... > 120 characters)". Did previous versions of PEP8 use 120 and PyCharm not update its PEP8 checker? I couldn't find any…
Samuel
  • 8,063
  • 8
  • 45
  • 41
55
votes
3 answers

How to tell flake8 to ignore comments

I'm using flake8 in emacs in order to clean up my python code. I find it annoying to have my comments flagged as errors (E501 line too long (x > 79 characters)). I'm wondering if anyone knows a way to kindly ask flake8 to ignore comments, both…
sacuL
  • 49,704
  • 8
  • 81
  • 106
54
votes
7 answers

Verifying PEP8 in iPython notebook code

Is there an easy way to check that iPython notebook code, while it's being written, is compliant with PEP8?
Seanny123
  • 8,776
  • 13
  • 68
  • 124
50
votes
4 answers

PyCharm shows "PEP8: expected 2 blank lines, found 1"

Consider the following code: def add_function(a, b): c = str(a) + b print "c is %s" % c def add_int_function(c, d): e = c + d print "the vaule of e is %d" % e if __name__ =="__main__": add_function(59906, 'kugrt5') …
march_seven
  • 691
  • 1
  • 7
  • 13
48
votes
14 answers

What PEP 8 guidelines do you ignore, and which ones do you stick to?

Over the years, the more Python I write, the more I find myself agreeing with most of the guidelines, though I consistently and intentionally break some for my own reasons. I'd be curious to know what in PEP 8 (or other PEPs too maybe) people…
user349594
48
votes
2 answers

Wrapping python doctest results that are longer than 80 characters

I'm trying to keep my source code under the 80 character guideline width that PEP8 recommends, but can't figure out how to wrap my doctest which has results longer than 80 characters. A noddy example: def long_string(): """ Returns a string…
pelson
  • 21,252
  • 4
  • 92
  • 99
47
votes
10 answers

Visual studio code suppress pep8 warnings

How can I suppress pep8 warnings, in Visual studio code? What I want to do is to suppress E501 warning I don't want to get warnings where my code length is more than 80 chars. I'm using Don Jayamanne's Python extension and here is my config file for…
latsha
  • 1,298
  • 1
  • 14
  • 22
1 2
3
55 56