PEP 7: Allow breaking lines before operators ("Knuth's style") (GH-3931)

As discussed in: https://discuss.python.org/t/62402

It doesn't make much sense for C and Python to be different here,
and the reasons for "Knuth's style" in PEP 8 apply to C code as well.
This commit is contained in:
Petr Viktorin 2024-09-09 16:02:27 +02:00 committed by GitHub
parent 337a97e9ab
commit 2dd6c9dfee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View File

@ -140,20 +140,24 @@ Code lay-out
"cannot create '%.100s' instances", "cannot create '%.100s' instances",
type->tp_name); type->tp_name);
* When you break a long expression at a binary operator, the * When you break a long expression at a binary operator, braces
operator goes at the end of the previous line, and braces should be should be formatted as shown:
formatted as shown. E.g.:
.. code-block:: .. code-block::
:class: good :class: good
if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 && if (type->tp_dictoffset != 0
type->tp_dictoffset == b_size && && base->tp_dictoffset == 0
(size_t)t_size == b_size + sizeof(PyObject *)) && type->tp_dictoffset == b_size
&& (size_t)t_size == b_size + sizeof(PyObject *))
{ {
return 0; /* "Forgive" adding a __dict__ only */ return 0; /* "Forgive" adding a __dict__ only */
} }
It's OK to put operators at ends of lines, especially to be
consistent with surrounding code.
(See :ref:`PEP 8 <pep8-operator-linebreak>` for a longer discussion.)
* Vertically align line continuation characters in multi-line macros. * Vertically align line continuation characters in multi-line macros.
* Macros intended to be used as a statement should use the * Macros intended to be used as a statement should use the

View File

@ -257,6 +257,9 @@ Another such case is with ``assert`` statements.
Make sure to indent the continued line appropriately. Make sure to indent the continued line appropriately.
.. _`pep8-operator-linebreak`:
Should a Line Break Before or After a Binary Operator? Should a Line Break Before or After a Binary Operator?
------------------------------------------------------ ------------------------------------------------------