<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, Jun 25, 2017 at 6:20 PM, Mikhail V <span dir="ltr"><<a href="mailto:mikhailwas@gmail.com" target="_blank">mikhailwas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">And it reminded me times starting with Python and  wondering<br>
why I can't simply write something like:<br>
<br>
def move(x,y):<br>
<span class="gmail-">    x = x + 10<br>
    y = y + 20<br>
</span>move(x,y)<br>
<br>
Instead of this:<br>
<br>
def move(x,y):<br>
    x1 = x + 10<br>
    y1 = y + 20<br>
    return x1,y1<br>
x,y = move(x,y)</blockquote><div><br></div><div>you CAN do that, if x and y are mutable types.</div><div><br></div><div>I've found that when folk want this behavior (often called "pass by reference" or something), what they really want in a mutable number. And you can make one of those if you like -- here's a minimal one that can be used as a counter:</div><br><font face="monospace, monospace">In [12]: class Mint():<br>    ...:     def __init__(self, val=0):<br>    ...:         self.val = val<br>    ...:        <br>    ...:     def __iadd__(self, other):<br>    ...:         self.val += other<br>    ...:         return self<br>    ...:    <br>    ...:     def __repr__(self):<br>    ...:         return "Mint(%i)" % self.val<br><br>so now your move() function can work:<br><br>In [17]: def move(x, y):<br>    ...:     x += 1<br>    ...:     y += 1<br>In [18]: a = Mint()<br>In [19]: b = Mint()<br><br>In [20]: move(a, b)<br><br><br>In [21]: a<br>Out[21]: Mint(1)<br><br>In [22]: b<br>Out[22]: Mint(1)</font><div><br></div><div>I've seen recipes for a complete mutable integer, though Google is failing me right now.</div><div><br></div><div>This does come up fairly often -- I usually think there are more Pythonic ways of solving the problem - like returning multiple values, but maybe it has its uses.</div><div><br></div><div>-CHB</div><div><br></div><div> <br></div></div>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>