Allow function to return multiple values
Hello Team, I am Joannah. I am currently working on a book on python compatibility and publishing it with apress. I have worked with python for a while we are talking about four years. 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. 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. I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is. Kind regards, Joannah -- Joannah Nanjekye +256776468213 F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah *"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
Why isn't a tuple enough? You can do automatic tuple unpack:
v1, v2 = return_multiplevalues(1, 2)
On Jun 1, 2017 17:18, "joannah nanjekye"
What is the difference between returning a tuple and returning two values? I think at least theoretically it's different wording for precisely the same thing. Elazar בתאריך יום ה׳, 1 ביונ' 2017, 17:21, מאת Markus Meskanen < markusmeskanen@gmail.com>:
Why isn't a tuple enough? You can do automatic tuple unpack:
v1, v2 = return_multiplevalues(1, 2)
On Jun 1, 2017 17:18, "joannah nanjekye"
wrote: Hello Team,
I am Joannah. I am currently working on a book on python compatibility and publishing it with apress. I have worked with python for a while we are talking about four years.
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.
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.
I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is.
Kind regards, Joannah
-- Joannah Nanjekye +256776468213 F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Thanks for response on automatic tuple unpack. My bad I dint know about
this all along.
Infact this works same way Go does. I have been analyzing why we would
really need such a function (allow function to return multiple types) in
python given we have this feature( automatic tuple unpack) and have not yet
got good ground. When I come across good ground I will talk about it.
So I will say this automatic tuple unpack pretty much works for my needs.
Thanks
On Thu, Jun 1, 2017 at 5:21 PM, Markus Meskanen
Why isn't a tuple enough? You can do automatic tuple unpack:
v1, v2 = return_multiplevalues(1, 2)
On Jun 1, 2017 17:18, "joannah nanjekye"
wrote: Hello Team,
I am Joannah. I am currently working on a book on python compatibility and publishing it with apress. I have worked with python for a while we are talking about four years.
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.
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.
I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is.
Kind regards, Joannah
-- Joannah Nanjekye +256776468213 F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Joannah Nanjekye +256776468213 F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah *"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
Welcome to the group, Joannah! Now that you've been introduced to packing and unpacking in Python, I would suggest learning the complete syntax, because it's a very useful feature.
a, b = "hi" # you can unpack any iterable a 'h' b 'i'
a, b = 1, 2 # this is the same as: a, b = (1, 2) a 1 b 2
a, (b, c) = [1, (2, 3)] # you can unpack nested iterables using parentheses first, *rest = "spam" # you can use '*' to capture multiple elements first 's' rest 'pam'
*rest, last = "eggs" # which elements are captured by `*` is implied by the other assignment targets rest 'egg' last 's'
first, second, *middle, before_last, last = "lumberjack" first 'l' second 'u' middle 'mberja' before_last 'c' last 'k'
a, b, *c = range(2) # a '*' variable can be empty c []
a, b, *c, d, e = range(3) # the number of non-star variables has to make sense ValueError
a, *b, c, *d = "african swallow" # multiple '*'s are FORBIDDEN! SyntaxError
a, *b = 1, 2, 3, 4, 5 # NOTE: Most itterables unpack starred variables as a list type(b)
a, *b = {1, 2, 3, 4, 5} type(b)
a, *b = [1, 2, 3, 4, 5] type(b)
a, *b = dict(zip("spam", range(4))) type(b)
a, *b = "except strings" type(b)
All of these rules apply just as well to assignment targets in for-loops:
for num, (first, *rest) in {1: "dead", 2: "parrot"}.items(): ... print("num=%r, first=%r, rest=%r"%(num, first, rest)) ... num=1, first='d', rest='ead' num=2, first='p', rest='arrot'
Hope that helps!
On Thu, Jun 8, 2017 at 7:22 AM, joannah nanjekye
Thanks for response on automatic tuple unpack. My bad I dint know about this all along.
Infact this works same way Go does. I have been analyzing why we would really need such a function (allow function to return multiple types) in python given we have this feature( automatic tuple unpack) and have not yet got good ground. When I come across good ground I will talk about it.
So I will say this automatic tuple unpack pretty much works for my needs.
Thanks
On Thu, Jun 1, 2017 at 5:21 PM, Markus Meskanen
wrote: Why isn't a tuple enough? You can do automatic tuple unpack:
v1, v2 = return_multiplevalues(1, 2)
On Jun 1, 2017 17:18, "joannah nanjekye"
wrote: Hello Team,
I am Joannah. I am currently working on a book on python compatibility and publishing it with apress. I have worked with python for a while we are talking about four years.
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.
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.
I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is.
Kind regards, Joannah
-- Joannah Nanjekye +256776468213 <+256%20776%20468213> F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Joannah Nanjekye +256776468213 <+256%20776%20468213> F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Thanks Abe for the insight.
On Thu, Jun 8, 2017 at 11:27 PM, Abe Dillon
Welcome to the group, Joannah!
Now that you've been introduced to packing and unpacking in Python, I would suggest learning the complete syntax, because it's a very useful feature.
a, b = "hi" # you can unpack any iterable a 'h' b 'i'
a, b = 1, 2 # this is the same as: a, b = (1, 2) a 1 b 2
a, (b, c) = [1, (2, 3)] # you can unpack nested iterables using parentheses first, *rest = "spam" # you can use '*' to capture multiple elements first 's' rest 'pam'
*rest, last = "eggs" # which elements are captured by `*` is implied by the other assignment targets rest 'egg' last 's'
first, second, *middle, before_last, last = "lumberjack" first 'l' second 'u' middle 'mberja' before_last 'c' last 'k'
a, b, *c = range(2) # a '*' variable can be empty c []
a, b, *c, d, e = range(3) # the number of non-star variables has to make sense ValueError
a, *b, c, *d = "african swallow" # multiple '*'s are FORBIDDEN! SyntaxError
a, *b = 1, 2, 3, 4, 5 # NOTE: Most itterables unpack starred variables as a list type(b)
a, *b = {1, 2, 3, 4, 5} type(b)
a, *b = [1, 2, 3, 4, 5] type(b)
a, *b = dict(zip("spam", range(4))) type(b)
a, *b = "except strings" type(b)
All of these rules apply just as well to assignment targets in for-loops:
for num, (first, *rest) in {1: "dead", 2: "parrot"}.items(): ... print("num=%r, first=%r, rest=%r"%(num, first, rest)) ... num=1, first='d', rest='ead' num=2, first='p', rest='arrot'
Hope that helps!
On Thu, Jun 8, 2017 at 7:22 AM, joannah nanjekye < nanjekyejoannah@gmail.com> wrote:
Thanks for response on automatic tuple unpack. My bad I dint know about this all along.
Infact this works same way Go does. I have been analyzing why we would really need such a function (allow function to return multiple types) in python given we have this feature( automatic tuple unpack) and have not yet got good ground. When I come across good ground I will talk about it.
So I will say this automatic tuple unpack pretty much works for my needs.
Thanks
On Thu, Jun 1, 2017 at 5:21 PM, Markus Meskanen
wrote:
Why isn't a tuple enough? You can do automatic tuple unpack:
v1, v2 = return_multiplevalues(1, 2)
On Jun 1, 2017 17:18, "joannah nanjekye"
wrote: Hello Team,
I am Joannah. I am currently working on a book on python compatibility and publishing it with apress. I have worked with python for a while we are talking about four years.
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.
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.
I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is.
Kind regards, Joannah
-- Joannah Nanjekye +256776468213 <+256%20776%20468213> F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Joannah Nanjekye +256776468213 <+256%20776%20468213> F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah
*"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Joannah Nanjekye +256776468213 F : Nanjekye Captain Joannah S : joannah.nanjekye T : @Captain_Joannah SO : joannah *"You think you know when you learn, are more sure when you can write, even more when you can teach, but certain when you can program." Alan J. Perlis*
On Thu, Jun 8, 2017 at 4:27 PM, Abe Dillon
a, *b = 1, 2, 3, 4, 5 # NOTE: Most itterables unpack starred variables as a list type(b)
a, *b = "except strings" type(b)
I was just playing around with this, and on Python 3.5.3, I see strings unpacked as lists:
first, *rest = 'spam' type(rest)
Am I doing something different, or is this something that changed in the language?
No. You're right. I don't know why I thought strings were treated
differently.
On Jun 9, 2017 7:47 AM, "Mark E. Haase"
On Thu, Jun 8, 2017 at 4:27 PM, Abe Dillon
wrote: a, *b = 1, 2, 3, 4, 5 # NOTE: Most itterables unpack starred variables as a list type(b)
a, *b = "except strings" type(b)
I was just playing around with this, and on Python 3.5.3, I see strings unpacked as lists:
first, *rest = 'spam' type(rest)
Am I doing something different, or is this something that changed in the language?
On 06/01/2017 07:17 AM, joannah nanjekye wrote:
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.
Why not just unpack the values? E.g. test.py ----------------- def f(a, b): return a, b a, b = f(1, 2) print(a) print(b) ----------------- $ python3 test.py 1 2 Cheers, Thomas
On 06/01/2017 07:17 AM, 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.
If you had a function that returned two values, how would you assign them? Maybe something like: var1, var2 = return_multiplevalues(num1, num2) ? That is exactly how Python works.
I will appreciate discussing this. You may also bring to light any current way of returning multiple values from a function that I may not know of in python if there is.
While I am somewhat alarmed that you don't know this already after four years of Python programming, I greatly appreciate you taking the time to find out. Thank you. -- ~Ethan~
On 6/1/2017 10:17 AM, 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.
Others have pointed out that you are not stuck at all. Returning a tuple that can be unpacked is Python's concrete implementation of the abstract concept 'return multiple values'. Note that Python's gives one a choice whether to keep the values bundles or to immediately unbundle them. -- Terry Jan Reedy
Hi Joannah, and welcome! On Thu, Jun 01, 2017 at 05:17:49PM +0300, joannah nanjekye wrote: [...]
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.
Can you explain how Go's multiple return values differ from Python's? Looking at the example here: https://golang.org/doc/effective_go.html it looks like there's very little difference. In Go, you have to declare the return type(s) of the function, which Python doesn't require, but the caller treats the function the same. The Go example is: func nextInt(b []byte, i int) (int, int) { for ; i < len(b) && !isDigit(b[i]); i++ { } x := 0 for ; i < len(b) && isDigit(b[i]); i++ { x = x*10 + int(b[i]) - '0' } return x, i } which the caller uses like this: x, i = nextInt(b, i) In Python, we would write exactly the same thing: inside the nextInt function, we'd return multiple values: return x, i and the caller would accept them like this: x, i = nextInt(b, i) So the syntax is even the same. How is Go different from Python? -- Steve
On 02/06/2017 01:11, Steven D'Aprano wrote:
Hi Joannah, and welcome!
On Thu, Jun 01, 2017 at 05:17:49PM +0300, joannah nanjekye wrote:
[...]
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.
Can you explain how Go's multiple return values differ from Python's? Looking at the example here:
https://golang.org/doc/effective_go.html
it looks like there's very little difference. In Go, you have to declare the return type(s) of the function, which Python doesn't require, but the caller treats the function the same.
I suggest you look up the Go reflect package and the use of interface{}, meaning that you can pass anything you like both into and out of a function. There lies The Road To Hell as far as I'm concerned, I'll stick with duck typing thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
participants (10)
-
Abe Dillon
-
Ethan Furman
-
joannah nanjekye
-
Mark E. Haase
-
Mark Lawrence
-
Markus Meskanen
-
Steven D'Aprano
-
Terry Reedy
-
Thomas Nyberg
-
אלעזר