<div dir="auto"><div><div class="gmail_quote"><div dir="ltr">On Sun, Aug 26, 2018, 9:24 PM James Lu <<a href="mailto:jamtlu@gmail.com">jamtlu@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">Hi Johnathan<br><br>I echo your points. Indeed, the PEP referenced to refers to a "tuple<br>expression" in the grammatical and not the programmatic sense.<br><br>Finally, here's something that surprised me a little bit<br><br>>>> x = [1, 2]; id(x)<br>140161160364616<br>>>> x += [3, 4]; id(x)<br>140161160364616<br><br>>>> x = (1, 2); id(x)<br>140161159928520<br>>>> x += (3, 4); id(x)<br>140161225906440<br><br>Notice that '+=' creates uses the same object when the object is<br>a<br>list, but creates a new object. This raises the question: Why and<br>how<br>does Python behave in this way?<br><br>It's because lists are mutable are tuples are immutable.<br>There's a dunder iadd method and a dunder add method.<br>iadd magic methods, operating on the left hand side, return None and<br>modify the object in-place. add magic methods return the result and<br>don't modify the object it's called on.<br>iadd is mutable add, whereas add is "return a copy with the result<br>added"<br><br>>>> tuple.__iadd__<br>Traceback (most recent call last):<br>File "<stdin>", line 1, in <module><br>AttributeError: type object 'tuple' has no attribute '__iadd__'<br>type object 'tuple' has no attribute '__iadd__'<br>>>> tuple.__add__<br><slot wrapper '__add__' of 'tuple' objects><br>>>> list.__iadd__<br><slot wrapper '__iadd__' of 'list' objects><br>>>> list.__add__<br><slot wrapper '__add__' of 'list' objects><br><br><br>tuple1 = tuple1.__add__(tuple2)<br><br>list1.__iadd__(list2)<br><br>> Does it IN PRACTICE bring sufficient benefits to users?<br><br>I found myself needing this when I was writing a monte-carlo<br>simulation in python that required incrementing a tallying counter<br>from a subroutine.<br></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">Wouldn't a numpy array be very suited for this kind of task?</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>