Python Memory Management and Tips Transcripts
Chapter: Memory and classes
Lecture: Plain ol' fields

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, let's jump back in and just start writing some code.
0:02 Go back to our project we're working on,
0:05 and we're gonna add another app, this is gonna be
0:08 "app_props_and_classes". We'll give that the fmain magic and set it up to
0:13 run. Alright, So I would like to be able to create a class.
0:19 We're going to keep working with this person idea,
0:22 that applies to all of them, but we're just gonna keep rolling with this idea of
0:25 a person. But I would like you to write a class that,
0:28 given that class, it's going to behave like this.
0:31 We'll be able to come over here and have a datetime,
0:36 which is the birthday. A monthly income,
0:38 which is a number, and we'll create a person,
0:40 Sarah Sampson, given the birthday and the monthly income,
0:44 and then it will have her full name,
0:45 which will be Sarah Sampson, Like that, from her two name parts.
0:50 How old she is in years based on right now,
0:52 her yearly income, how long until she retires and so on.
0:56 So we're gonna do this three different times.
0:59 The first one we're going to create what I'll call the "naive person object",
1:03 and that is just write this in the most straightforward way that you would with Python
1:07 classes. And then we're gonna make one improvement and another.
1:11 So, I'm gonna create a little place to store those three classes over here.
1:16 I'll call this "props_people". I wouldn't normally organize this,
1:20 I'd just call it person or something.
1:21 We've already got several persons rolling around. And we're gonna have a naive, let's say
1:27 "PersonNaive" like this, and let's just remind ourselves what that was that we're going after.
1:36 We'll come over here and we'll create an initializer, a constructor,
1:41 and it's gonna have to take first name,
1:44 last name, birthdate and monthly income.
1:52 Those are the things that are passing in.
1:55 This one's gonna be an integer.
1:57 This will be a datetime,
2:01 datetime, like so, and strings and strings.
2:09 Then let's just have PyCharm
2:10 Add the fields to the class.
2:13 So thank you pyCharm for doing that.
2:16 I do wish that it would not do it in reverse order that it would put
2:19 them behind, not ahead. But you know,
2:22 So it goes. We can always reorder it. Like I feel like that
2:27 should go up and those should probably go above, I don't know why.
2:30 So, so far that gets us down here.
2:34 And now we're gonna have a full name and we'd like to be able to say
2:37 "object.full_name", so let's put that here. We'll say "self.full_name",
2:44 we'll just make an f-string, "self.first_name" we're not gonna need the "slef" at this point.
2:49 First, last. Alright, that's gonna be the full name.
2:52 Ah, then we'll take care of this one.
2:55 We need age in years. Well,
3:02 what we need to know about age in years we're gonna need to hold on to
3:05 now. So we'll say "datetime.datetime.now()", call that function,
3:11 and now is gonna be the time it is now.
3:14 So, age in years, let's see,
3:16 we gotta go from their birthday over to this.
3:19 So we'll say, I'll make an "age_delta" is gonna be now minus birthdate,
3:30 and this is gonna give us a time delta,
3:32 which represents basically the seconds or the ticks.
3:35 And there's a cool thing we could do with time
3:36 deltas, we can divide them, and I can come over here and say "days equals 365"
3:46 and that's gonna take however long that is and convert it to however many years. So over here
3:50 we'll say, maybe we'll store it as an integer.
3:54 People think of themselves as having an integer age.
3:58 You know, I'm not 36.271111 I'm you know,
4:02 whatever, you know, 36. Or,
4:05 sometimes you round up a little early,
4:06 but you talk about that in integers.
4:08 So integers we're gonna put there. Yearly income, again.
4:13 This will be "self.yearly_income",
4:15 that's 12 times the monthly income.
4:18 That one was easy. And finally,
4:22 years until retirement. So "self.years_to_retire" is gonna be max
4:28 of zero, because you might already be up for retirement, or "self.age_in_years",
4:34 let's say 65 minus that, because
4:39 at least in the U.S.,
4:40 typically people retire at 65 on average.
4:42 So, how long until you're 65 years old?
4:46 Alright, I think that this might do it.
4:49 Let's go over here and we'll say "props_people import this one". Now, notice
4:59 This is a person. We're gonna have different variations.
5:01 So here's the thing that is not normally done in programming,
5:04 but I'm going to do here.
5:05 I'm gonna come over and redefine,
5:08 have a person type,
5:11 which is gonna be a person_naive at first.
5:15 And then we'll have a global person type, nut we can change this to affect other
5:18 functions. So in the beginning,
5:21 we're going to start with this.
5:22 We could even say
5:23 testing, or, "running with person implementation: {person.__name__}" if this was and f-string, like that.
5:36 And then what we want to do is we just want to run "retirement_summary()"
5:41 right? That's a lot of work,
5:43 but it set the stage for this exploration.
5:46 Maybe I'll spell this correctly rather than PyCharm
5:49 spell it correctly. Let's go ahead and run and see what happened.
5:53 Yes! It works! so hi
5:55 Sarah Sampson, let's review your retirement.
5:57 Your 46 years old. You currently make $84,000 USD a year.
6:02 You have 19 until retirement. Maybe units belong there.
6:06 I'll be sure to put some bank, some money in the bank and retire for retirement
6:09 each month. Um, there you go, 19 years,
6:14 we're gonna say one years, but that's fine.
6:16 I'm happy with that,
6:17 it's okay. Good enough for our purpose here.
6:21 So this type of definition of this object here seems totally natural,
6:26 right? It lets us go to our person,
6:30 say person dot, we have age_in_years,
6:31 last, first, full_name, all the things that we want to work with and
6:35 notice these are "f's" so these are fields
6:38 as opposed to functions or other types of things,
6:42 this is great. What could go wrong?
6:44 Well, it turns out it's not super efficient,
6:46 even if it is the most natural way to do things.