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
45
votes
6 answers

What is the recommended way to break a long if statement? (W504 line break after binary operator)

What is currently the recommended way to break a long line of if statement with "and" and "or" operators? 1st option With the style below (which is from PEP8) with flake8, I'm getting warnings: W504 line break after binary operator: if…
ann.piv
  • 681
  • 1
  • 7
  • 17
45
votes
2 answers

Is it wrong to use the "==" operator when comparing to an empty list?

PyCharm (4.0.6) complains when I do a comparison to an empty list using the == operator, but it doesn't when I use the is operator: I guess this is something related to PEP 8, but the problem is that when I use the is operator, as PyCharm suggests,…
renatov
  • 5,005
  • 6
  • 31
  • 38
45
votes
3 answers

Python alignment of assignments (style)

I really like following style standards, as those specified in PEP 8. I have a linter that checks it automatically, and definitely my code is much better because of that. There is just one point in PEP 8, the E251 & E221 don't feel very good. Coming…
bgusach
  • 14,527
  • 14
  • 51
  • 68
44
votes
3 answers

Does PEP 8 require whitespace around operators in function arguments?

I have this code: some_list = range(a, b+1) After checking my coding style with pep8 plugin for vim, I got this warning: missing whitespace around operator It seems that to be compliant with PEP 8 I should instead write this? some_list = range(a,…
wxl24life
  • 683
  • 1
  • 7
  • 13
41
votes
5 answers

Pycharm's code style inspection: ignore/switch off specific rules

I'm trying to import existing project into PyCharm. I can refactor the code so that PyCharm will be pleased, but we like to have spaces around colons in dictionaries, like this: {"A" : "B"}. We also like aligning assignments: a = 1 abc = 3 Is…
Krzysztof Stanisławek
  • 1,267
  • 4
  • 13
  • 27
40
votes
3 answers

What is the naming convention for Python class references

What is the naming convention for a variable referencing a class in Python? class MyClass(object): pass # which one is correct? reference_to_class = MyClass # or ReferenceToClass = MyClass Here is another example that resembles my…
Peter Hudec
  • 2,462
  • 3
  • 22
  • 29
39
votes
1 answer

Change indentation level in Google Colab

I am using Google Colab to write Python code in their notebooks. Whenever I hit enter after a loop or conditional, the new line is automatically indented, which is good, but it uses only 2 whitespaces by default. This is contrary to the PEP-8…
zabop
  • 6,750
  • 3
  • 39
  • 84
38
votes
2 answers

Python for-loop without index and item

Is it possible in python to have a for-loop without index and item? I have something like the following: list_1 = [] for i in range(5): list_1.append(3) The code above works fine, but is not nice according to the pep8 coding guidelines. It…
jonie83
  • 1,136
  • 2
  • 17
  • 28
38
votes
2 answers

PEP8 naming convention on test classes

I have been looking at PEP 8 -- Style Guide for Python Code and PEP8 -- Advanced Usage for clues on how to name my test classes. However, this is never mentioned on both sites, as well as many other sites I have looked at, such as the unittest page…
musabaloyi
  • 651
  • 1
  • 7
  • 11
37
votes
1 answer

How can I configure Pylint to check all things PEP8 checks?

Searching for an answer on Pylint's mailing list brings no interesting results. Pylint is known to be very customizable, so I guess this should be possible... The reason I would like Pylint to check compliance with PEP8 is because PyDev has much…
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
37
votes
6 answers

PEP8 and PyQt, how to reconcile function capitalization?

I'm starting to use PyQt in some projects and I'm running into a stylistic dilemma. PyQt's functions use camel case, but PEP8, which I prefer to follow, says to use underscores and all lowercase for function names. So on the one hand, I can…
Colin
  • 10,447
  • 11
  • 46
  • 54
37
votes
4 answers

How to disable special naming convention inspection of PEP 8 in PyCharm

I installed PyCharm and enabled pep8 checks in Inspections. If I write: def func(argOne): print(argOne) The IDE shows me this warning: Argument name should be lowercase There is no option to ignore only such inspection. I cant find such error…
ya_dimon
  • 3,483
  • 3
  • 31
  • 42
36
votes
1 answer

Are docstrings for internal functions (python) necessary?

In python we designate internal function/ private methonds with an underscore at the beginning. Should these functions be documented with docstrings(is it required?)? (the formal documentation i mean, not the one helping the code-reader to…
0xc0de
  • 8,028
  • 5
  • 49
  • 75
34
votes
4 answers

PEP8: conflict between W292 and W391

As far as I know in unix it's a good practice to always have blank line at the end of file - or to put it in other words: every line should end with \n. While checking my python code with PEP8 I noticed that it also states that there should be \n at…
seler
  • 8,803
  • 4
  • 41
  • 54
33
votes
4 answers

How to fix Pylint "Wrong hanging indentation" and PEP8 E121?

I am trying to properly indent following piece of code: RULES_LIST = [ ('Name1', 1, 'Long string upto 40 chars'), ('Name2', 2, 'Long string upto 40 chars'), ('Name3', 3, 'Long string upto 40 chars'), ('Name4', 4, 'Long string upto 40…
Jatin Kumar
  • 2,635
  • 9
  • 36
  • 46