From 2dd6c9dfee13933d89f436afe7929fd8a1263390 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 9 Sep 2024 16:02:27 +0200 Subject: [PATCH] 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. --- peps/pep-0007.rst | 16 ++++++++++------ peps/pep-0008.rst | 3 +++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/peps/pep-0007.rst b/peps/pep-0007.rst index bcad808ee..f85ef7182 100644 --- a/peps/pep-0007.rst +++ b/peps/pep-0007.rst @@ -140,20 +140,24 @@ Code lay-out "cannot create '%.100s' instances", type->tp_name); -* When you break a long expression at a binary operator, the - operator goes at the end of the previous line, and braces should be - formatted as shown. E.g.: +* When you break a long expression at a binary operator, braces + should be formatted as shown: .. code-block:: :class: good - if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 && - type->tp_dictoffset == b_size && - (size_t)t_size == b_size + sizeof(PyObject *)) + if (type->tp_dictoffset != 0 + && base->tp_dictoffset == 0 + && type->tp_dictoffset == b_size + && (size_t)t_size == b_size + sizeof(PyObject *)) { 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 ` for a longer discussion.) + * Vertically align line continuation characters in multi-line macros. * Macros intended to be used as a statement should use the diff --git a/peps/pep-0008.rst b/peps/pep-0008.rst index 783093c98..087370fed 100644 --- a/peps/pep-0008.rst +++ b/peps/pep-0008.rst @@ -257,6 +257,9 @@ Another such case is with ``assert`` statements. Make sure to indent the continued line appropriately. + +.. _`pep8-operator-linebreak`: + Should a Line Break Before or After a Binary Operator? ------------------------------------------------------