1 00:00:00,000 --> 00:00:03,120 Class attributes are just a really fancy way of saying it's 2 00:00:03,120 --> 00:00:04,800 a variable inside of a class. 3 00:00:05,500 --> 00:00:07,200 Why we don't just call them variables? 4 00:00:07,220 --> 00:00:09,670 Well, it's because a variable can live outside of a class, 5 00:00:09,680 --> 00:00:12,730 and a variable inside of a class is usually scoped to it. 6 00:00:12,740 --> 00:00:15,460 So it has a special name because it has a special purpose. 7 00:00:15,460 --> 00:00:17,800 So we call them attributes. 8 00:00:17,800 --> 00:00:20,890 Now, they're typically used with the 'self' keyword, which 9 00:00:20,900 --> 00:00:24,360 we saw in the Introduction to Object Oriented Programming 10 00:00:24,360 --> 00:00:27,900 video. But we're going to dive into that a little bit more in this video. 11 00:00:27,940 --> 00:00:30,720 So whenever you're working inside of a class, and you see 12 00:00:30,730 --> 00:00:35,520 'self.', some variable name, as soon as you see 'self.', you 13 00:00:35,530 --> 00:00:39,640 can just assume that that is an attribute, not a variable. 14 00:00:39,650 --> 00:00:40,870 It's a variable type. 15 00:00:40,870 --> 00:00:44,870 It's a subset of a variable called an attribute. 16 00:00:44,870 --> 00:00:49,200 So let's go ahead and create our first class. So 'class MyClass', 17 00:00:49,200 --> 00:00:53,500 and in here we have this magical function called '__init__'. 18 00:00:53,670 --> 00:00:57,250 So we're going to create just one method here, one function, 19 00:00:57,250 --> 00:01:01,500 and it starts with '__init__', 20 00:01:01,500 --> 00:01:03,600 and it takes 'self' by default. 21 00:01:03,640 --> 00:01:08,440 Now, anytime you see '__', or a double '_', 22 00:01:08,450 --> 00:01:10,600 or as we call it in Python, a dunder, 23 00:01:10,900 --> 00:01:16,300 anytime you see a dunder, you can assume that that is built into Python. 24 00:01:16,300 --> 00:01:20,050 And so this is going to have some sort of magic power behind 25 00:01:20,060 --> 00:01:23,340 it, something that we don't yet, just out of the box, something 26 00:01:23,350 --> 00:01:26,850 that we get to work with that Python gives us, but we should 27 00:01:26,860 --> 00:01:28,920 not try to change it by any means. 28 00:01:28,920 --> 00:01:30,200 We should work with it. 29 00:01:30,240 --> 00:01:33,300 So in here, I'm just going to say 'pass' for now, and what 30 00:01:33,300 --> 00:01:35,500 I want to do, is just add a variable. 31 00:01:35,500 --> 00:01:39,400 So let's run this for now, and we've got 'my_class = 32 00:01:39,400 --> 00:01:42,900 MyClass', and instantiate it with parentheses. 33 00:01:42,900 --> 00:01:45,120 But 'my_class' currently has no attributes. 34 00:01:45,130 --> 00:01:47,820 It doesn't have a name, it doesn't have a purpose, it doesn't 35 00:01:47,820 --> 00:01:48,900 do anything. 36 00:01:49,600 --> 00:01:51,400 So let's add our first parameter in here. 37 00:01:51,450 --> 00:01:54,830 So 'param1' is going to be anything we want. 38 00:01:55,100 --> 00:01:58,300 And I'm just going to keep this simple and very generic for the time being. 39 00:01:58,320 --> 00:02:03,730 So we could 'print(param1)', and let's run that, and run 40 00:02:03,730 --> 00:02:07,500 that, and we're going to see that we're missing a positional 41 00:02:07,500 --> 00:02:09,400 argument, just like a function. 42 00:02:09,400 --> 00:02:12,000 If a function is looking for an argument and it doesn't get 43 00:02:12,000 --> 00:02:15,100 it, you're going to see a very, very similar error. 44 00:02:15,100 --> 00:02:21,720 So let's just call this, "The first param", and run that, 45 00:02:21,730 --> 00:02:23,220 and it prints out the first parameter. 46 00:02:23,900 --> 00:02:27,100 Now, if I want that attribute, if I want to be able to use 47 00:02:27,190 --> 00:02:31,900 it from my variable here, I would like to use 'my_class. 48 00:02:31,900 --> 00:02:33,900 param1', and this is not going to work. 49 00:02:34,900 --> 00:02:36,900 And the reason for that is because 'my_class' doesn't have 50 00:02:36,900 --> 00:02:38,600 an attribute called 'param1'. 51 00:02:38,620 --> 00:02:43,330 It does inside of '__init__', but it does not inside of the entire 52 00:02:43,330 --> 00:02:46,000 class. Again, that comes down to variable scoping. 53 00:02:46,060 --> 00:02:48,310 'param1' doesn't exist anywhere. 54 00:02:48,320 --> 00:02:52,270 So what we do to make it exist in this entire class, and not 55 00:02:52,270 --> 00:03:00,400 just in here, but also in here, we can say 'self.param1 = param1'. 56 00:03:00,400 --> 00:03:02,900 Now, this looks a little weird because we're basically assigning 57 00:03:02,900 --> 00:03:04,600 the variable to itself, 58 00:03:04,700 --> 00:03:07,880 but that's just something we need to do in order to get access 59 00:03:07,890 --> 00:03:10,440 to that parameter through our variable. 60 00:03:10,450 --> 00:03:15,230 So if we rerun this, that, and that, it will now say, "The first 61 00:03:15,230 --> 00:03:19,900 param". Alternatively, you can add things by default. 62 00:03:19,900 --> 00:03:24,100 So in here, just underneath your class, you could type 'course 63 00:03:24,100 --> 00:03:27,100 = "Python for Everybody"', 64 00:03:27,100 --> 00:03:30,000 and we do not need to run 65 00:03:30,000 --> 00:03:33,200 'self.course = course', because that's already in here. 66 00:03:33,200 --> 00:03:35,400 We've already defined the attribute. 67 00:03:35,420 --> 00:03:43,620 And so if we run this, this, this, and then do 'my_class.course', it 68 00:03:43,620 --> 00:03:44,900 prints out the 'course'. 69 00:03:44,940 --> 00:03:47,610 Now, you might be thinking, "Well, how come I have to use 70 00:03:47,610 --> 00:03:50,400 'self' here, but I don't have to use 'self' here". 71 00:03:50,900 --> 00:03:52,000 That's a good question, 72 00:03:52,000 --> 00:03:54,000 and we're going to see this a little bit later. 73 00:03:54,000 --> 00:03:56,000 But if you have another function in here, 74 00:03:56,000 --> 00:03:58,400 [no audio] 75 00:03:58,400 --> 00:04:02,300 what 'self' does is, 'self' basically says, "Hey, 'param1'". 76 00:04:02,360 --> 00:04:07,160 It's essentially like saying 'param1 =', default 77 00:04:07,160 --> 00:04:10,820 value. And so whenever you see 'self.', and then the variable 78 00:04:10,830 --> 00:04:13,350 name or the attribute name, what it's actually saying is, 79 00:04:13,350 --> 00:04:15,750 "Hey, jump outside of this function here. 80 00:04:15,760 --> 00:04:17,220 Just jump just outside of it, 81 00:04:17,230 --> 00:04:19,890 not outside of the object, but just jump outside of the function, 82 00:04:20,100 --> 00:04:23,899 grab that variable name, and assign it a new value". 83 00:04:25,100 --> 00:04:28,000 And so in other methods and other functions, we can use 84 00:04:28,000 --> 00:04:32,500 'self.param1', and we can use 'self.course'. 85 00:04:32,620 --> 00:04:37,660 Now, this gets a little beyond the scope of this particular 86 00:04:37,670 --> 00:04:40,930 lesson, but what I would like you to do just to get some 87 00:04:40,940 --> 00:04:43,780 hands on experience here, is I want you to create a new class, 88 00:04:44,020 --> 00:04:45,100 could be named anything, 89 00:04:45,110 --> 00:04:49,960 remember, camel casing, assign a regular parameter here just 90 00:04:49,960 --> 00:04:55,200 inside of the class, but not inside of a method, not inside of a function, 91 00:04:55,200 --> 00:04:58,100 then create the magic '__init__' method. 92 00:04:58,100 --> 00:04:59,500 Remember, two '_'. 93 00:04:59,500 --> 00:05:03,900 Every function inside of a class always starts with 'self', 94 00:05:03,900 --> 00:05:07,700 and then your first parameter, assign 'self.param1' 95 00:05:07,770 --> 00:05:09,470 to whatever that parameter is going to be. 96 00:05:09,600 --> 00:05:14,300 Instantiate your class, and then use whatever your variable name is, 97 00:05:14,370 --> 00:05:19,610 in my case, it's 'my_class.param1', and print that out, 98 00:05:19,610 --> 00:05:22,300 and then print out 'my_class.course'. 99 00:05:22,340 --> 00:05:24,170 So go ahead, write that out by hand. 100 00:05:24,180 --> 00:05:26,300 It will give you a little extra muscle memory 101 00:05:26,310 --> 00:05:29,310 when it comes to understanding how all of this works. 102 00:05:29,310 --> 00:05:32,200 This whole thing should only take you about 2 or 3 minutes. 103 00:05:32,260 --> 00:05:35,890 Once you've done that, let's head on over to that next Object Oriented 104 00:05:35,900 --> 00:05:37,400 Programming lesson.