Re: [Python-ideas] Allow function to return multiple values
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@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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
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/
On Sun, Jun 25, 2017 at 6:20 PM, Mikhail V <mikhailwas@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)
Instead of this:
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 ...: ...: def __iadd__(self, other): ...: 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@noaa.gov
participants (3)
-
Chris Barker
-
Mikhail V
-
rymg19@gmail.com