21

I'm doing code review, and seeing method declarations like that:

def __init__(self,data):

I always thought that it should be formatted like this:

def __init__(self, data):

But then I checked PEP 0008 and see no exact statement about that. There is guide about whitespace around operators, and inside parentheses, but no about comma separated list.

If it is not described in PEP8, probably there is some unwritten convention about this? Why I was convinced that this was in PEP8? Shoud PEP8 be updated?

Quill
  • 2,729
  • 1
  • 33
  • 44
Bunyk
  • 7,635
  • 8
  • 47
  • 79
  • 3
    I'm not posting this as an answer since I can't find the source to verify, but I remember reading that the reasoning behind this is that Python should (when it can) mimic English grammar and rules. ie. A list of things in English is comma separated in the same format is in Python. The intention is to aid readability and familiarity with the syntax. – SuperBiasedMan Jul 31 '15 at 08:45

1 Answers1

17

I can't find the corresponding sentence in PEP8 as well, but I guess the reason that most people believe this rule is in PEP8 is pip pep8.

According to their document:

E231 missing whitespace after ‘,’

As most people use this as their style checker, it is easy to be convinced that the rule is really in PEP8.

Gary Sham
  • 503
  • 4
  • 10
  • 3
    Technically correct that it has no individual rule, but it is a style adhered to in the style guide itself. One might argue that the recommendation against space between a trailing comma and closing parenthesis is the [exception that proves the rule](https://en.wikipedia.org/wiki/Exception_that_proves_the_rule). – Yann Vernier Aug 22 '17 at 16:16
  • Having the whitespace there seems to be consistent with the fact that you have whitespaces between elements of an iterable. So if you have a comma in the fist place, might as well have a space. Seems to be easier to read too. – radtek Oct 29 '20 at 19:46