[Python-ideas] Alternative spelling for list.append()

Mikhail V mikhailwas at gmail.com
Mon Jun 18 17:55:04 EDT 2018


On Mon, Jun 18, 2018 at 11:43 PM, Michael Selik <mike at selik.org> wrote:
> On Mon, Jun 18, 2018 at 12:56 PM Mikhail V <mikhailwas at gmail.com> wrote:
>>
>> Numpy arrays have also append() and insert() methods,
>
> In [2]: np.arange(1).append(2)
> AttributeError: 'numpy.ndarray' object has no attribute 'append'
>

https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html

>> So one syntax generalization possible is towards insert() method.
>
>
> Why would you even want to encourage inserting into a list? It's slow and
> should be *discouraged*.

I don't - but I have seen it in real projects and modules. and
ther is Deque.appendleft()


> On Mon, Jun 18, 2018 at 1:08 PM Joao S. O. Bueno <jsbueno at python.org.br>
> wrote:
>>
>> MutableSequence protocol to define "<<" as "append"
>
>
> No one has demonstrated with any realistic examples why code would look
> better with ``a <<= b`` instead of ``a.append(b)``.

So you have 2 separate inquiries in one: explaining why and where is
the example.
Why it is so - I have tried to explain several times and also in the summary,
with a small example (see first post in this thread - 'transposed_row' example).

As for examples - below is one example from 'pyparsing' module.
But I don't advise to think about "why" but rather just relax and
try to 'traverse' the code back and forth several times.
(and sometimes it's better to treat things just as an advice if you doubt
you can figure it out by yourself - that's not adressed to you but just
general life observation)


---------------- with <<=

    out = []
    NL = '\n'
    out <<=  indent + _ustr(self.asList())
    if full:
        if self.haskeys():
            items = sorted((str(k), v) for k,v in self.items())
            for k,v in items:
                if out:
                    out <<=  NL
                out <<=  "%s%s- %s: " % (indent,('  '*depth), k)
                if isinstance (v,ParseResults):
                    if v:
                        out <<=  v.dump(indent,depth+1)
                    else:
                        out <<=  _ustr(v)
                else:
                    out <<=  repr(v)


-----------------  with  .append()

    out = []
    NL = '\n'
    out.append( indent+_ustr(self.asList()) )
    if full:
        if self.haskeys():
            items = sorted((str(k), v) for k,v in self.items())
            for k,v in items:
                if out:
                    out.append(NL)
                out.append( "%s%s- %s: " % (indent,('  '*depth), k) )
                if isinstance(v,ParseResults):
                    if v:
                        out.append( v.dump(indent,depth+1) )
                    else:
                        out.append(_ustr(v))
                else:
                    out.append(repr(v))


More information about the Python-ideas mailing list