<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jun 18, 2018 at 6:20 PM Juancarlo Añez <<a href="mailto:apalala@gmail.com">apalala@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_extra">For all practical purpose, it would be enough to define that the expression:<br></div></div></div><span class="m_5552179492180601138gmail-"><div dir="ltr"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_extra">mylist += [item]</div></div></blockquote><div class="gmail_extra"><div class="gmail_extra">gets optimized to mylist.append(item).</div></div></div></span></blockquote><div><br></div><div>Unfortunately, that would create yet another special case of operators breaking the rules. Most operators invoke magic methods. This would prevent ``+=`` from invoking ``__iadd__`` for lists, since the right-hand side would need to be compiled differently. It's similar to why ``and`` and ``or`` keywords can't have magic methods.</div></div></div>
</blockquote></div><div class="gmail_extra"><br></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_extra">It seems that the optimization is already in place:</div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><br></div><div class="gmail_extra">def main():</div><div class="gmail_extra"> print(timeit.timeit('using_append()', globals=globals(), number=REPS))</div><div class="gmail_extra"> print(timeit.timeit('using_concat()', globals=globals(), number=REPS))</div><div class="gmail_extra"> print(timeit.timeit('using_iadd()', globals=globals(), number=REPS))</div></div></div></blockquote></div></blockquote><div> </div><div>I'm not intimately familiar with the opcodes, but I believe that any code involving the expression ``[x]`` will build a list.</div><div><br></div><div><div>In [2]: dis.dis("a += [x]")</div><div> 1 0 LOAD_NAME 0 (a)</div><div> 2 LOAD_NAME 1 (x)</div><div> 4 BUILD_LIST 1</div><div> 6 INPLACE_ADD</div><div> 8 STORE_NAME 0 (a)</div><div> 10 LOAD_CONST 0 (None)</div><div> 12 RETURN_VALUE</div></div><div><br></div><div>I didn't run the timings, but I wouldn't be surprised if building a one-element list is faster than looking up an attribute. Or vice-versa.</div><div><br></div><div>I thought you meant that you wanted to change the syntax such that ``a += [x]`` would, if the left-hand is a list, use different opcodes.</div></div></div>