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

Michael Selik mike at selik.org
Mon Jun 18 21:59:45 EDT 2018

On Mon, Jun 18, 2018 at 2:55 PM Mikhail V <mikhailwas at gmail.com> wrote:

> 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

Perhaps NumPy chose not to provide an append method to discourage repeated
appends, since it's terribly slow.

It's a good design philosophy: make inefficient code look appropriately

> On Mon, Jun 18, 2018 at 1:08 PM Joao S. O. Bueno <jsbueno at python.org.br>
> 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.

That's interesting advice. I contrast, I find it's best to aggressively ask
"Why?" and to rewrite the code in order to fully understand it.

I think I found the chunk of code you're referring to.

First, I'll note that ``dump`` usually indicates dumping to a file, while
``dumps`` is dumping to a str. Regardless, I think this ``dump`` function
could be improved a bit, and a revision reduces the benefit of a dedicated
``append`` operator.

    if not full:
        return indent + _ustr(self.asList())

    lines = [_ustr(self.asList())]

    if self.haskeys():
        fmt = '  ' * depth + '- %s: %s'
        for k, v in sorted((str(k), v) for k, v in self.items()):
            if isinstance(v, ParseResults):
                if v:
                    s = v.dump(indent, depth + 1)
                    s = _ustr(v)
                s = repr(v)
            lines.append(fmt % (k, s))

    elif any(isinstance(v, ParseResults) for v in self):
        for i, v in enumerate(self):
            lines.append('  ' * depth + '[%d]:' % i)
            if isinstance(v, ParseResults):
                s = v.dump(indent, depth + 1)
                s = _ustr(v)
            lines.append('  ' * (depth + 1) + s)

    return ('\n' + indent).join(lines)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180618/87726a53/attachment.html>

More information about the Python-ideas mailing list