Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Tuples
Lecture: Multiple return values from a function
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Sometimes you need to return more than one value from a function. Let's see what the story of that is in Python.
0:08
So over here we have a non-Pythonic way to return more than one value from a function,
0:12
now in Python, we don't have the concept of reference parameters, passing a pointer by reference,
0:20
we just have passing the pointer, which lets us work with the values.
0:23
Some languages you can do things like this, you can say "int & val 1, int & val 2",
0:30
that would be like C++, and say C# you might say like this: "out" or "ref" or "in out", things like this, we could even, if this was pass by value,
0:42
we could even pass a pointer and then let it change, there is lots of things that some languages let us do, Python doesn't let us do that.
0:49
So here is one way which we can kind of do this in a kind of a hokey way so here we are passing in some value to work with,
0:55
we want to compute 2 values and return both the values, so here we are going to pass in a list,
1:00
and if the list is empty we are going to make a spot for 2 entries, otherwise, if the list is not length 2, we are going to complain
1:06
and say "Oh this is not really what we are looking for", we wanted either a list with 2 elements
1:10
or an empty list so that we can stuff the two return values into them. Do a quick little bit of math and back here we get the values out
1:18
and we pull them out, this is super non-Pythonic. This is bad, so the question is: "Can we do better?" First of all, let's see if we pass in 7
1:28
that we are going to get the right values. 49 and 18.52 Those are right values, but the code, not so right, so let's take this and have a good version.
1:38
Keep that one down here, we'll make this one to be Pythonic, we'll just call it out_params, and we are going to do something entirely different.
1:44
We are going to get rid of all the stuff, this link, all this junk, watch how much simpler this gets. So we'll have, let's say, return value 1,
1:54
those are not good names in general for variables but maybe just to make a case of look these are the two values we are returning, we'll call of this.
2:01
So we can come down here and we can return a tuple and we can say that just say "r1, r2", that defines a tuple, that's one thing, we'll return that.
2:10
That's a little bit like what we were doing before with our list, so we could like say "return a list" but the thing that's cool
2:17
is the tuple unpacking lets us get at that value really easy, so we can come over here and we can say we would like to call this function
2:23
out_params with the value 7, we'd like to capture the values, remember, it's coming back as a tuple, so we can unpack that into individual values
2:30
and give basically the appearance that our method is returning more than one values,
2:35
we can say "v1, v2 = this", I can print out let's say the good version instead of this funky stashing stuff in the list, we just say v1, v2.
2:45
Now it literally looks like this method returns more than one value, but the trick that facilitates it is of course tuples and tuple unpacking.
2:53
Perfect, besides a little bit of spacing, it looks identical. There, identical, so much better.
3:01
So we saw we can fiddle with collection types to make it sort of possible
3:07
to return more than one value, this is really a bad idea, don't do this kind of stuff,
3:11
instead, leverage the ability to create and return as single tuple and then unpack them as if they were multiple values,
3:18
so here we are calling compute values, return it to tuple, we are unpacking that into two variables we are calling "b2", and "b32",
3:25
and we are printing them out. Wonderful.