On 12/21/2025 11:54 AM, Chris Angelico via Python-list wrote:
On Mon, 22 Dec 2025 at 03:19, Peter J. Holzer <[email protected]> wrote:
On 2025-12-21 23:59:48 +1100, Chris Angelico via Python-list wrote:
On Sun, 21 Dec 2025 at 23:31, Peter J. Holzer <[email protected]> wrote:
There is the concept of "pythonic" Code (just like there is a concept of
idiomatic code in (almost) any language), but if you ask 100 randomly
chosen Python programmers whether the use of += is pythonic, I doubt
that more than a handful would say no.
I would expect the opposite. It's in the language for a reason. Is it
somehow more Pythonic to repeat yourself?
I think we agree, but my double negative may have been confusing.
My expectation is:
at most 5% say "no, using += is not pythonic"
at least 95% say "yes, using += is pythonic"
Yeah. I'm not going to try to predict specific percentages, but I
believe a significant majority of Python programmers would say that
yes, += IS Pythonic.
ChrisA
Augmented operators like "+=" were introduced to Python by PEP 0203, in
the year 2000 for Python 2.0, so they have been around for a long time.
From the PEP:
"The idea behind augmented assignment in Python is that it isn’t just an
easier way to write the common practice of storing the result of a
binary operation in its left-hand operand, but also a way for the
left-hand operand in question to know that it should operate on itself,
rather than creating a modified copy of itself."
So the syntax is more than a mere shortcut. More:
"Writing the above expression as:
<x> <operator>= <y>
is both more readable and less error prone, because it is instantly
obvious to the reader that it is <x> that is being changed, and not <x>
that is being replaced by something almost, but not quite, entirely
unlike <x>."
And something I hadn't thought about but especially important these days:
"The new in-place operations are especially useful to matrix calculation
and other applications that require large objects. In order to
efficiently deal with the available program memory, such packages cannot
blindly use the current binary operations. Because these operations
always create a new object, adding a single item to an existing (large)
object would result in copying the entire object (which may cause the
application to run out of memory), add the single item, and then
possibly delete the original object, depending on reference count."
So I would say that "+=" is Pythonic.
--
https://mail.python.org/mailman3//lists/python-list.python.org