[Python-ideas] Operator for inserting an element into a list

Mikhail V mikhailwas at gmail.com
Tue Jun 12 19:09:16 EDT 2018


On Tue, Jun 12, 2018 at 10:25 PM, Michael Selik <mike at selik.org> wrote:
> On Tue, Jun 12, 2018 at 11:08 AM Mikhail V <mikhailwas at gmail.com> wrote:
>>
>> On Tue, Jun 12, 2018 at 7:42 PM, Clint Hepner <clint.hepner at gmail.com>
>> wrote:
>>
>> So the idea was about an insert/append operator.
>> Which would use augmented operator. The operator may be
>> /= or ^=. (I like ^= more, so I'll put here example with it).
>
>
> The "/" operator means divide. How do you divide a list?
> The "^" operator means exclusive-or. Again, strange concept for a list.
>

Allowed standard characters are so - they already mean something and
there are so few (even less if throw away ugly looking).

For me the plus character + means sum of numbers.
So for an array:

    A += 1

Means for me unequivocally increment each element of array by 1.
And this:

    A += B

Means for me increment each element of A by corresponding values of B.
BTW that is how plus operator works for Numpy arrays.

So I don't think it is a precise thing how logical this or that character suits
an operation. Everybody understands that overloading of operators is
for convenience and it's just a shortcut for some frequent usage.

As I understand, overloading some operators for certain object type (list in
this case) has relatively low cost in terms of implementation.

As for me - I'm fine with append() method and use it all the time.
Frankly speaking, I was working on another syntax idea and
it turned out that actually augmented operators may be very useful for
that particular syntax design.


>
>>
>> L += var
>> and
>> L = L + var
>>
>> are different, so it seems to me they were not meant to be
>> bound together.
>
>
> One is mutation, the other isn't, but aside from that, the result is
> equivalent.

You're right of course. I was just confused by the fact that

L += "aa"                # works
L = L + "aa"            # gives TypeError

But for consistent types both should work the same.


> I think you'll find it's
> natural once you get used to slice assignment.

I use slice assignment all the time with Numpy arrays, though for list element
appending I prefer append() method.



M


More information about the Python-ideas mailing list