
On 26 June 2017 at 01:14, rymg19@gmail.com <rymg19@gmail.com> wrote:
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.
Can be so, though it was not quite clear. The original OP's example function included same variables as input and output and the phrasing "I noticed that this actually returns a tuple of the values which I did not want in the first place" actually can indicate that my theory can be also valid. 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) Instead of this: def move(x,y): x1 = x + 10 y1 = y + 20 return x1,y1 x,y = move(x,y) So probably there was some corellation with this and OP's ideas, IDK.
On Jun 25, 2017 at 6:09 PM, <Mikhail V> 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@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/