Coding style in CPython implementation

Chris Angelico rosuav at gmail.com
Sat Oct 28 15:17:07 EDT 2017


On Sun, Oct 29, 2017 at 5:42 AM, Στέφανος Σωφρονίου
<stefanossofroniou542 at gmail.com> wrote:
> Greetings everyone.
>
> I have noticed that in many if conditions the following syntax is used:
>
> a) if (variable == NULL) { ... }
> b) if (variable == -1) { ... }
> c) if (variable != NULL) { ... }
>
> What I wanted to ask is, is there a particular reason for not choosing
>
> a) if (!variable) { ... } in place of if (variable == NULL) { ... },
> b) if (-1 == variable) { ... } in place of if (variable == -1) { ... }, and
> c) if (variable) { ... } in place of if (variable) { ... } ?
>
> Especially the (b) syntax is extremely dangerous to assign -1 to variable in case of an accidental mistyping of equals sign; it had happened countless times by now to to many of us that use various C-family languages.
>
> Is there a particular reason for using this specific coding style?

Have you read PEP 7?

https://www.python.org/dev/peps/pep-0007/

PEP 7 and PEP 8 are a pair of style guides that govern the CPython
source code - PEP 7 for the C code, and PEP 8 for the Python code in
the standard library. Unfortunately, many people seem to think that
PEP 8 is supposed to govern *their* code, and as such, end up not even
realizing that PEP 7 exists to answer all the same sorts of questions.

ChrisA



More information about the Python-list mailing list