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
994
votes
9 answers

Python `if x is not None` or `if not x is None`?

I've always thought of the if not x is None version to be more clear, but Google's style guide and PEP-8 both use if x is not None. Are there any minor performance differences (I'm assuming not), and is there any case where one really doesn't fit…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
534
votes
22 answers

Should import statements always be at the top of a module?

PEP 8 states: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. However if the class/method/function that I am importing is only used in rare cases, surely it is…
Adam J. Forster
  • 17,789
  • 9
  • 25
  • 20
406
votes
2 answers

Pylint, PyChecker or PyFlakes?

I would like to get some feedback on these tools on: features; adaptability; ease of use and learning curve.
Bite code
  • 578,959
  • 113
  • 301
  • 329
396
votes
8 answers

How do I set the maximum line length in PyCharm?

I am using PyCharm on Windows and want to change the settings to limit the maximum line length to 79 characters, as opposed to the default limit of 120 characters. Where can I change the maximum amount of characters per line in PyCharm?
Ansuman Bebarta
  • 7,011
  • 6
  • 27
  • 44
380
votes
4 answers

What does '# noqa' mean in Python comments?

While searching through a Python project, I found a few lines commented with # noqa. import sys sys.path.append(r'C:\dev') import some_module # noqa What does noqa mean in Python? Is it specific to Python only?
Ishpreet
  • 5,230
  • 2
  • 19
  • 35
372
votes
3 answers

Should I use "camel case" or underscores in Python?

So which is better and why? def my_function(): or def myFunction():
tdc
  • 8,219
  • 11
  • 41
  • 63
352
votes
2 answers

What is PEP8's E128: continuation line under-indented for visual indent?

Just opened a file with Sublime Text (with Sublime Linter) and noticed a PEP8 formatting error that I'd never seen before. Here's the text: urlpatterns = patterns('', url(r'^$', listing, name='investment-listing'), ) It's flagging the second…
Oli
  • 235,628
  • 64
  • 220
  • 299
317
votes
9 answers

Why does PEP-8 specify a maximum line length of 79 characters?

Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters? Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the…
pcorcoran
  • 7,894
  • 6
  • 28
  • 26
269
votes
13 answers

How to write very long string that conforms with PEP8 and prevent E501

As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e. s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten." How would I go…
Federer
  • 33,677
  • 39
  • 93
  • 121
261
votes
6 answers

What's the correct way to sort Python `import x` and `from x import y` statements?

The python style guide suggests to group imports like this: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports However, it does not mention anything…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
200
votes
18 answers

Why does Python pep-8 strongly recommend spaces over tabs for indentation?

I see on Stack Overflow and PEP 8 that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain. Is there an underlying reason for spaces to be…
quamrana
  • 37,849
  • 12
  • 53
  • 71
182
votes
9 answers

How to break a line of chained methods in Python?

I have a line of the following code (don't blame for naming conventions, they are not mine): subkeyword = Session.query( Subkeyword.subkeyword_id, Subkeyword.subkeyword_word ).filter_by( subkeyword_company_id=self.e_company_id ).filter_by( …
Juliusz Gonera
  • 4,658
  • 5
  • 32
  • 35
165
votes
4 answers

Is there a recommended format for multi-line imports?

I have read there are three ways for coding multi-line imports in python With slashes: from Tkinter import Tk, Frame, Button, Entry, Canvas, Text, \ LEFT, DISABLED, NORMAL, RIDGE, END Duplicating senteces: from Tkinter import Tk, Frame, Button,…
Manuel Alvarez
  • 2,029
  • 3
  • 14
  • 17
160
votes
2 answers

Chained method calls indentation style in Python

From reading PEP-8, I get it that you should put the closing parenthesis on the same line as the last argument in function calls: ShortName.objects.distinct().filter( product__photo__stockitem__isnull=False) Probably, long expressions are best…
katspaugh
  • 17,449
  • 11
  • 66
  • 103
144
votes
6 answers

"assert" statement with or without parentheses

Here are four simple invocations of assert: >>> assert 1==2 Traceback (most recent call last): File "", line 1, in ? AssertionError >>> assert 1==2, "hi" Traceback (most recent call last): File "", line 1, in ? AssertionError:…
new name
  • 15,861
  • 19
  • 68
  • 114
1
2 3
55 56