1 00:00:00,000 --> 00:00:03,395 [MUSIC PLAYING] 2 00:00:03,395 --> 00:00:05,335 3 00:00:05,335 --> 00:00:09,310 CARTER ZENKE: Well, hello one and all, and welcome to our short on instance 4 00:00:09,310 --> 00:00:10,310 variables. 5 00:00:10,310 --> 00:00:13,840 In a prior short on defining classes, we saw a way 6 00:00:13,840 --> 00:00:18,970 to create a class or a template for a package, one what we called, in fact, 7 00:00:18,970 --> 00:00:19,790 package. 8 00:00:19,790 --> 00:00:22,990 And down below, we created several instances 9 00:00:22,990 --> 00:00:27,940 of that class of that template to present individual packages here. 10 00:00:27,940 --> 00:00:32,020 So notice on lines 11 and 12, I have here some code 11 00:00:32,020 --> 00:00:37,610 that defines for me two instances of this class that we called package. 12 00:00:37,610 --> 00:00:40,790 It's a bit like me creating a template for something called a package 13 00:00:40,790 --> 00:00:44,750 and down below creating two particular packages. 14 00:00:44,750 --> 00:00:46,970 Now, I did so as follows. 15 00:00:46,970 --> 00:00:51,380 I said down below that this first package has a number one. 16 00:00:51,380 --> 00:00:53,170 The sender was Alice. 17 00:00:53,170 --> 00:00:54,790 The recipient was Bob. 18 00:00:54,790 --> 00:00:57,290 And the weight here was 10. 19 00:00:57,290 --> 00:01:02,390 But what actually happens as we call some code on line 11? 20 00:01:02,390 --> 00:01:06,990 Well, we said we'd come back to these lines, 3 through 6 here. 21 00:01:06,990 --> 00:01:09,860 So let's take a look at exactly what's happening. 22 00:01:09,860 --> 00:01:13,970 Well, when I run some line of code, like on line 11, 23 00:01:13,970 --> 00:01:19,500 I again am creating some new instance of this class I've defined called package. 24 00:01:19,500 --> 00:01:22,730 And along the way, thanks to Python, this method 25 00:01:22,730 --> 00:01:25,260 called dunder init is called. 26 00:01:25,260 --> 00:01:28,550 Now, we said here that it takes as input self, which 27 00:01:28,550 --> 00:01:32,780 refers to the new instance of the class that we are creating. 28 00:01:32,780 --> 00:01:36,980 And it seems like what I'm doing here is taking in various inputs, 29 00:01:36,980 --> 00:01:41,090 like number, sender, recipient, and weight, and assigning them 30 00:01:41,090 --> 00:01:44,960 to various attributes or properties of this class, 31 00:01:44,960 --> 00:01:47,270 of this instance of the class here. 32 00:01:47,270 --> 00:01:49,730 So it turns out that each of these things, 33 00:01:49,730 --> 00:01:55,290 self.number, self.sender, and so on, are called instance variables. 34 00:01:55,290 --> 00:01:59,210 They're variables that belong to some instance of this class. 35 00:01:59,210 --> 00:02:03,810 And they are defined for me whenever I call package down below. 36 00:02:03,810 --> 00:02:06,210 So let's look at number, for instance. 37 00:02:06,210 --> 00:02:08,009 Number equals 1. 38 00:02:08,009 --> 00:02:14,670 Well, I'm now passing in to dunder init this argument, number, with the value 1. 39 00:02:14,670 --> 00:02:17,460 And what happens to that number 1? 40 00:02:17,460 --> 00:02:20,120 Well, it actually gets assigned as an instance variable 41 00:02:20,120 --> 00:02:22,770 within this instance of the class. 42 00:02:22,770 --> 00:02:28,700 So this instance here has a variable called number and so on for sender, 43 00:02:28,700 --> 00:02:30,750 recipient, and weight. 44 00:02:30,750 --> 00:02:33,720 But let's see this in action here. 45 00:02:33,720 --> 00:02:38,060 I'll come down below, and let me try to actually access these instance 46 00:02:38,060 --> 00:02:42,660 variables that I claim I set up on lines 3 through 6. 47 00:02:42,660 --> 00:02:46,970 Well, I could imagine, let's say, looping through every package 48 00:02:46,970 --> 00:02:50,450 that I have inside of this list of packages, maybe 49 00:02:50,450 --> 00:02:52,390 printing them out along the way. 50 00:02:52,390 --> 00:02:55,040 Well, to do so, I could use a for loop. 51 00:02:55,040 --> 00:02:57,810 I want to print for each package I have. 52 00:02:57,810 --> 00:03:01,010 So I'll say for package in the list of packages. 53 00:03:01,010 --> 00:03:03,590 And then I'll fill this in a little later. 54 00:03:03,590 --> 00:03:08,210 Notice first that package will first refer to the package on line 11 55 00:03:08,210 --> 00:03:12,440 and then, on the next iteration, the package on line 12. 56 00:03:12,440 --> 00:03:15,900 So very simple here, let's start off by printing something. 57 00:03:15,900 --> 00:03:18,380 We could print the package as a whole, but I don't 58 00:03:18,380 --> 00:03:19,860 think we'd get a very good output. 59 00:03:19,860 --> 00:03:24,290 What I could do instead is try printing, in this case, something 60 00:03:24,290 --> 00:03:28,610 like the instance variable that we called number, just to start with, 61 00:03:28,610 --> 00:03:31,130 package.number. 62 00:03:31,130 --> 00:03:35,720 So if I want to access an instance variable, I can, in this case, 63 00:03:35,720 --> 00:03:41,630 refer to the individual instance of the class, package here, followed by dot, 64 00:03:41,630 --> 00:03:43,910 followed by the instance variable's name, 65 00:03:43,910 --> 00:03:47,610 just like we set up above, in this case, number. 66 00:03:47,610 --> 00:03:49,260 So if I try this-- 67 00:03:49,260 --> 00:03:51,560 I'll try Python of packages.py. 68 00:03:51,560 --> 00:03:52,650 Hit Enter. 69 00:03:52,650 --> 00:03:54,900 We'll see 1 and 2. 70 00:03:54,900 --> 00:03:59,580 But just to hammer this home, let me try instead 1 and 3 71 00:03:59,580 --> 00:04:03,180 and now hopefully see 1 and 3. 72 00:04:03,180 --> 00:04:06,980 So it does seem, as I defined in dunder init, 73 00:04:06,980 --> 00:04:11,270 that the input to these various parameters of package, 74 00:04:11,270 --> 00:04:15,290 these arguments here, they actually get assigned as instance variables 75 00:04:15,290 --> 00:04:18,290 of each instance of my class. 76 00:04:18,290 --> 00:04:22,430 Now let's try accessing more and maybe printing 77 00:04:22,430 --> 00:04:26,610 these packages in a prettier way. 78 00:04:26,610 --> 00:04:28,530 So here I can do this. 79 00:04:28,530 --> 00:04:33,590 I could go back to the format I had before but now use a Python f string 80 00:04:33,590 --> 00:04:36,360 in the packages instance variables. 81 00:04:36,360 --> 00:04:41,330 I could say package and then package.number, just like this. 82 00:04:41,330 --> 00:04:46,500 If I were to run this, I could see package 1 and package 2, pretty good. 83 00:04:46,500 --> 00:04:49,530 What if I tried maybe the same format from before? 84 00:04:49,530 --> 00:04:52,400 I could try colon, and then I could enter in, 85 00:04:52,400 --> 00:04:59,730 let's say, the package's sender to package.recipient. 86 00:04:59,730 --> 00:05:01,620 And let's see what we see here. 87 00:05:01,620 --> 00:05:03,650 Python packages.py. 88 00:05:03,650 --> 00:05:05,720 Package 1, Alice to Bob. 89 00:05:05,720 --> 00:05:08,100 Package 2, Bob to Charlie. 90 00:05:08,100 --> 00:05:12,050 And this is all happening by accessing these instance variables 91 00:05:12,050 --> 00:05:16,010 that I set up in dunder init as well. 92 00:05:16,010 --> 00:05:17,450 Let's now add in the weight. 93 00:05:17,450 --> 00:05:20,550 I'll say comma package.weight. 94 00:05:20,550 --> 00:05:24,690 And we said the weight was, in this case, in kilograms. 95 00:05:24,690 --> 00:05:28,610 I'll go ahead and now try to run this again. 96 00:05:28,610 --> 00:05:32,600 I'll go ahead and go back to my terminal-- python of packages.py-- 97 00:05:32,600 --> 00:05:37,700 and we'll see a much prettier way of formatting these individual packages. 98 00:05:37,700 --> 00:05:42,260 In fact, we can get the benefits of the strings we saw, let's say, 99 00:05:42,260 --> 00:05:46,920 in the short on defining classes, which allows them to be more prettily printed. 100 00:05:46,920 --> 00:05:49,520 But we also get the benefits of this class here, 101 00:05:49,520 --> 00:05:52,670 this way of encapsulating information now using 102 00:05:52,670 --> 00:05:55,500 these things called instance variables. 103 00:05:55,500 --> 00:05:58,170 This then was our short on instance variables. 104 00:05:58,170 --> 00:06:00,880 And we'll see you next time. 105 00:06:00,880 --> 00:06:02,000