Python Memory Management and Tips Transcripts
Chapter: Memory and functions
Lecture: Useful closures
0:00 Let's look at another example, and this time we're gonna talk about an aspect of
0:04 Python that is super cool, is not used that often,
0:07 but is probably often misunderstood, and that is closures.
0:11 So let's go over here and create an app. We'll call it
0:14 "useful_closures". We're gonna have two types of closures.
0:18 This one is like the most standard normal thing I can think of.
0:22 So we'll have a fmain thing going on,
0:24 and what I want to do is I want to create a person class. Instead of
0:29 using the one we had before, I'm just going to create a named tuple. So we'll say
0:31 "person equals namedtuple", and you got to say the name of the type,
0:39 should probably be the same. And then the things that it has,
0:41 it has a name and an age.
0:45 So that's like defining a class but less effort,
0:48 probably less memory as well. I'm not sure.
0:50 So let's go down here and create some people. And here we'll say it's a person, and
0:55 PyCharm is awesome, it knows it takes a name and an age, that's pretty cool.
0:58 So let's say "Sarah, 28".
1:04 Let's throw Lilly in here, she's 24. Throw in a guy named Matt,
1:11 he's 32. Zoe, she's 40. And Jack, young Jack,
1:21 he's just 10. OK, so these are our people.
1:23 What I want to do, I want to show them.
1:25 So, print out the people and let's go ahead and tell this it's gonna run.
1:30 And look, we've got Sarah, and then Lilly, and then Matt, and
1:34 you know, guess what? That's the order in which they were put there.
1:37 But what if we sort them? Where they're lists,
1:42 lists can be sorted. That's pretty awesome.
1:44 How is it sorted? Hmmm.
1:46 It is gonna be sorted alphabetically.
1:49 First, its by name, and then it's going to sort by age.
1:53 So if we had two Jakes, right?
1:55 a Jake that is
1:57 50, and a Jake that is 10.
2:01 Where are our Jakes? See,
2:02 it put the younger ones, so where there's a match on the name it actually goes and
2:07 then looks at the second thing in the tuple.
2:08 That's actually killer. That's really,
2:10 really cool. But what if I don't want to do that?
2:12 What if I want to sort just by age?
2:14 Well, then we're gonna sort it in a different way.
2:17 We're going to do something really cool.
2:18 We're gonna use a lambda expression.
2:19 so we'll say "print, default sort".
2:22 And let's do a better sort.
2:24 So we'll say "print, sort by age",
2:29 so let's do the same thing, "people.sort", but notice
2:31 now there's this key, and the key is a function.
2:36 We can give it a regular function or a lambda expression,
2:39 and it gives you one of the items in here.
2:40 And then what you're supposed to do is return the thing you're gonna sort by, that
2:44 passes it to the underlying quick sort, and then it sorts the real object.
2:47 So we could just say "p.age".
2:50 And here we'll say "print, people".
2:54 So here we sort by age. We have Jack, and then Lily, and then Sarah,
2:58 check that out. If we wanted to have reversed,
2:59 we could put a negative here,
3:02 or we could say reversed, Right?
3:03 But you can control just by like what you pass back here.
3:06 Now, Old Jake is first,
3:07 and then Zoe and so on.
3:10 So this is cool. We use it all the time in Python to do neat
3:13 stuff. But what if you need more information?
3:16 What if you want to sort by some criteria?
3:19 Like, for younger people, I want to sort them from youngest to oldest, but older
3:24 people, I want to sort them from oldest to youngest.
3:27 Something like that, alright?
3:29 How could you accomplish that over here? Sort by age.
3:35 Grouped, I guess we'll call it?
3:37 I don't know. Where do we pass more information? So the p,
3:41 the p comes from sort passing.
3:44 When it calls the key function,
3:45 it passes one of the items and says,
3:47 "give me the key", given one of the items in the list. And then it's
3:50 gonna use the sort, right?
3:53 Where's our additional information? So what if we wanted to say,
3:56 Oh, there's a cutoff,
3:58 and if you're over 30, we're going to show the oldest people first, sort descending. If
4:02 you're gonna be under 30, we're gonna show the newest, so sort ascending.
4:07 What we can do is, instead of passing a function, a variable to this function,
4:12 we can capture it through a closure.
4:15 Okay, so what we can say is
4:16 "we want this, if p.age is greater than cutoff. else,
4:21 we're just going to sort ascending with p.age".
4:24 So if you're over 30, we're gonna sort you descending,
4:27 if you're under 30, we're gonna sort you ascending.
4:32 How is this variable passed to this function
4:35 when it gets called? You can see PyCharm
4:37 thinks they're the same. It's highlighting it. If we run it,
4:40 we should be getting what we're looking for.
4:42 Let me do a little separator. Over here, we have for the old people,
4:47 Jake, and then Zoe, and then Matt.
4:50 And then it flips. For the younger people,
4:52 it's Jake, young Jake, and then Lily, and then Sarah.
4:56 So the younger people are sorted ascending.
4:58 The older people are sorted descending.
5:00 This is completely confusing and you would never do it as a way of organizing your
5:04 data, probably. The point is to show that if you wanted to do something non-standard,
5:08 something extra, when provided additional information,
5:13 you can take this variable, pass it off over there,
5:17 and then call that function, right?
5:19 The only parameters being passed to this function
5:21 are p, but it actually is working with two parts of data: p and cutoff.
5:26 That's awesome. So this concept here is a closure. And this ability to
5:31 have functions like reach around them and grab onto these is really interesting.
5:36 It's practical in cases like this where I just need a little extra information to decide
5:41 something, and then off it goes.
5:44 We'll see that this can lead to problems with memory.
5:47 But for now, let's just take a step back and see how cool it is
5:50 that we can pass along these extra pieces of data through a side channel called "closures".