# [Python-ideas] Allow function to return multiple values

Chris Barker chris.barker at noaa.gov
Wed Jul 5 14:34:25 EDT 2017

```On Sun, Jun 25, 2017 at 6:20 PM, Mikhail V <mikhailwas at gmail.com> wrote:

> And it reminded me times starting with Python and  wondering
> why I can't simply write something like:
>
> def move(x,y):
>     x = x + 10
>     y = y + 20
> move(x,y)
>
>
> def move(x,y):
>     x1 = x + 10
>     y1 = y + 20
>     return x1,y1
> x,y = move(x,y)

you CAN do that, if x and y are mutable types.

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:

In [12]: class Mint():
...:     def __init__(self, val=0):
...:         self.val = val
...:
...:         self.val += other
...:         return self
...:
...:     def __repr__(self):
...:         return "Mint(%i)" % self.val

so now your move() function can work:

In [17]: def move(x, y):
...:     x += 1
...:     y += 1
In [18]: a = Mint()
In [19]: b = Mint()

In [20]: move(a, b)

In [21]: a
Out[21]: Mint(1)

In [22]: b
Out[22]: Mint(1)

I've seen recipes for a complete mutable integer, though Google is failing
me right now.

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.

-CHB

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170705/4c713d71/attachment.html>
```