[Python-Dev] PEP 7 and braces { .... } on if

Victor Stinner victor.stinner at gmail.com
Wed May 31 10:13:58 EDT 2017


Previous discussion which added "strongly preferred" to the PEP 7, January 2016:
https://mail.python.org/pipermail/python-dev/2016-January/142746.html

Victor

2017-05-31 16:11 GMT+02:00 Victor Stinner <victor.stinner at gmail.com>:
> Hi,
>
> I have a question on the CPython coding code for C code, the PEP 7:
> https://www.python.org/dev/peps/pep-0007/
>
> """
> Code structure: (...); braces are strongly preferred but may be
> omitted where C permits, and they should be formatted as shown:
>
> if (mro != NULL) {
>     ...
> }
> else {
>     ...
> }
> """
>
> This section was updated after the creation of CPython in 1991, so as
> you may expect, a lot of existing C code doesn't respect this coding
> style. I'm trying to slowly add "missing" braces and indent as the
> example *when I have to modify existing code*.
>
> The problem is the "strongly preferred" and "omitted where C permits"
> part. I would like to make the PEP 7 more explicit on that part since
> in each review, I disagree with Serhiy who wants to omit them.
> Example:
>
> if (func == NULL)
>     return NULL;
>
> versus
>
> if (func == NULL) {
>     return NULL;
> }
>
> I now prefer the version with braces. It's more verbose, but it
> prevents bugs when the code is modified later for whatever reasons.
> IMHO it also makes the code easily to read and to understand.
>
> So I would suggest to modify the PEP 7 to *always* require braces for if.
>
> I would also suggest to require braces on "for(...) { ... }" and
> "while(...) { ... }". But only if the code has to be modified, not
> only to update the coding style.
>
> Changes which are pure coding style changes should be rejected to
> avoid conflicts with other pending pull requests and "code churn".
>
> Victor


More information about the Python-Dev mailing list