[Python-ideas] Allow function to return multiple values

Mikhail V mikhailwas at gmail.com
Sun Jun 25 19:09:02 EDT 2017


joannah nanjekye wrote:


> [...]
>
>Today I was writing an example snippet for the book and needed to write a
>function that returns two values something like this:
>
>def return_multiplevalues(num1, num2):
>     return num1, num2
>
> I noticed that this actually returns a tuple of the values which I did not
>want in the first place.I wanted python to return two values in their own
>types so I can work with them as they are but here I was stuck with working
>around a tuple.

It was quite puzzling at first what was the actual idea but probably I
can guess why this question came up by you.
It seems to me (I am just intuitively guessing that) that you were about to
write a procedure which operates on global variables.
If so you should use the keyword "global" for that.
E.g. if you want to work with the variables defined in other
part of the code you can simply do it:

x = 0
y = 0
def move():
    global x, y
    x = x + 10
    y = y + 20
move()
print (x,y)

This function will change x and y (global variables in this case).
Note that without the line with the "global" statement this will not work.
Another typical usage is initialising variables inside a procedure:

def init_coordinates():
    global x,y
    x=0
    y=0
init_coordinates()
print (x,y)


So for some reason it seemed to me that you are trying to do
something like that.

>My proposal is we provide a way of functions returning multiple values.
>This has been implemented in languages like Go and I have found many cases
>where I needed and used such a functionality. I wish for this convenience
>in python so that I don't  have to suffer going around a tuple.

So if using globals as in the above examples you certinly don't
have to suffer going around a tuple.



Mikhail


More information about the Python-ideas mailing list