[Python-ideas] Allow function to return multiple values

rymg19 at gmail.com rymg19 at gmail.com
Sun Jun 25 19:14:18 EDT 2017


IIRC I'm pretty sure the OP just didn't know about the existence of tuple
unpacking and the ability to use that to return multiple values.


--
Ryan (ライアン)
Yoko Shimomura, ryo (supercell/EGOIST), Hiroyuki Sawano >> everyone
elsehttp://refi64.com

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

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
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170625/88694969/attachment.html>


More information about the Python-ideas mailing list