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