[Python-ideas] Operator for inserting an element into a list
Chris Barker
chris.barker at noaa.gov
Thu Jun 14 12:40:19 EDT 2018
On Wed, Jun 13, 2018 at 6:45 PM, Mikhail V <mikhailwas at gmail.com> wrote:
> Another point is that people do like augmented operators much and for the
> append - there are so many advises like: hey, use L += [item] !
>
another data point -- in teaching, a number of newbie students do exactly
that.
Actually, they do:
a_list += an_item
and find it does not do what they want, and then the get confused, and I
show that they need:
a_list += [an_item]
or
a_list.append(an_item)
(this gets particularly confusing them an_item is, itself, a sequence (a
string is really common).
So it would be nice to have an operator version of append -- but given the
limited number of operators, and their usual uses, I suspect it would cause
even more confusion...
But throwing it out there, how about (ab)using the mat_mul operator:
a_list @= an_item
Another note:
One of the major motivations for augmented assignment was being able to
support in-place operations for numpy:
an_array += something
which was MUCH nicer notation that was was required:
np.sum(an_array, something, out=an_array)
That is a much bigger win than going from .append() to an operator.
-CHB
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180614/a441effb/attachment.html>
More information about the Python-ideas
mailing list