<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jun 18, 2018 at 2:55 PM Mikhail V <<a href="mailto:mikhailwas@gmail.com">mikhailwas@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, Jun 18, 2018 at 11:43 PM, Michael Selik <<a href="mailto:mike@selik.org" target="_blank">mike@selik.org</a>> wrote:<br>
> On Mon, Jun 18, 2018 at 12:56 PM Mikhail V <<a href="mailto:mikhailwas@gmail.com" target="_blank">mikhailwas@gmail.com</a>> wrote:<br>
>> Numpy arrays have also append() and insert() methods,<br>
> In [2]: np.arange(1).append(2)<br>
> AttributeError: 'numpy.ndarray' object has no attribute 'append'<br>
<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html" rel="noreferrer" target="_blank">https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html</a></blockquote><div><br></div><div>Perhaps NumPy chose not to provide an append method to discourage repeated appends, since it's terribly slow.</div><div><br></div><div>It's a good design philosophy: make inefficient code look appropriately ugly.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> On Mon, Jun 18, 2018 at 1:08 PM Joao S. O. Bueno <<a href="mailto:jsbueno@python.org.br" target="_blank">jsbueno@python.org.br</a>><br>As for examples - below is one example from 'pyparsing' module.<br>
But I don't advise to think about "why" but rather just relax and<br>
try to 'traverse' the code back and forth several times.<br></blockquote><div><br></div><div>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.</div><div><br></div><div>I think I found the chunk of code you're referring to.</div><div><a href="https://github.com/pyparsing/pyparsing/blob/master/pyparsing.py#L848">https://github.com/pyparsing/pyparsing/blob/master/pyparsing.py#L848</a><br></div><div><br></div><div>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.</div><div><br></div><div><div>    if not full:</div><div>        return indent + _ustr(self.asList())</div><div><br></div><div>    lines = [_ustr(self.asList())]</div><div><br></div><div>    if self.haskeys():</div><div>        fmt = '  ' * depth + '- %s: %s'</div><div>        for k, v in sorted((str(k), v) for k, v in self.items()):</div><div>            if isinstance(v, ParseResults):</div><div>                if v:</div><div>                    s = v.dump(indent, depth + 1)</div><div>                else:</div><div>                    s = _ustr(v)</div><div>            else:</div><div>                s = repr(v)</div><div>            lines.append(fmt % (k, s))</div><div>            </div><div>    elif any(isinstance(v, ParseResults) for v in self):</div><div>        for i, v in enumerate(self):</div><div>            lines.append('  ' * depth + '[%d]:' % i)</div><div>            if isinstance(v, ParseResults):</div><div>                s = v.dump(indent, depth + 1)</div><div>            else:</div><div>                s = _ustr(v)</div><div>            lines.append('  ' * (depth + 1) + s)</div><div><br></div><div>    return ('\n' + indent).join(lines)</div></div><div><br></div><div><br></div><div><br></div><div> </div></div></div>