1 00:00:00,437 --> 00:00:01,192 Ready to see my solution? 2 00:00:01,192 --> 00:00:03,750 Let's dig in. 3 00:00:03,750 --> 00:00:06,977 The first step was to create a class called Treehouse. 4 00:00:06,977 --> 00:00:09,344 So I'm gonna do a couple spaces here. 5 00:00:09,344 --> 00:00:17,911 And let's do class, and remember the name of the class should be capitalized, so Treehouse. 6 00:00:17,911 --> 00:00:20,595 Oops, I need two S's there. 7 00:00:20,595 --> 00:00:21,894 There we go. 8 00:00:21,894 --> 00:00:26,416 Okay, inside of our class, we need to add 9 00:00:26,416 --> 00:00:31,457 a class attribute called product, product, 10 00:00:31,457 --> 00:00:36,513 and set it equal to the string coding courses. 11 00:00:36,513 --> 00:00:37,433 And you know what? 12 00:00:37,433 --> 00:00:43,268 I'm just gonna copy this, copy, paste. 13 00:00:43,268 --> 00:00:44,269 Perfect, two steps done. 14 00:00:44,269 --> 00:00:49,883 Three, add an initializer method that takes a parameter called name. 15 00:00:49,883 --> 00:00:53,030 I'm gonna put a space here, and 16 00:00:53,030 --> 00:00:58,869 our initializer method is our def, define, dunder init. 17 00:00:58,869 --> 00:01:03,307 And you can see it kinda adds some stuff automatically here, 18 00:01:03,307 --> 00:01:05,752 thanks to my Visual Studio Code. 19 00:01:05,752 --> 00:01:11,689 And inside, it's going to always take self and we're gonna add name. 20 00:01:11,689 --> 00:01:16,314 So number three complete. 21 00:01:16,314 --> 00:01:17,481 Number four, 22 00:01:17,481 --> 00:01:23,521 add a name instance attribute set equal to the parameter being passed in. 23 00:01:23,521 --> 00:01:26,091 So what we wanna do is set an instance attribute, 24 00:01:26,091 --> 00:01:29,111 which are attributes inside of our initializer method. 25 00:01:29,111 --> 00:01:33,879 And we wanna set our name attribute that's being passed in. 26 00:01:33,879 --> 00:01:38,683 So self.name is our attribute, instance attribute, and 27 00:01:38,683 --> 00:01:44,359 we're setting that equal to name that's being passed in, perfect. 28 00:01:46,514 --> 00:01:48,349 Number four is complete. 29 00:01:48,349 --> 00:01:51,855 Number five, we're going to create a method called learn. 30 00:01:51,855 --> 00:01:54,240 Let's do that step first. 31 00:01:54,240 --> 00:01:59,462 Add another space here, def, and method is called learn. 32 00:01:59,462 --> 00:02:01,992 All methods inside of classes will need to take self. 33 00:02:01,992 --> 00:02:07,371 Inside, the method should return 34 00:02:07,371 --> 00:02:12,574 a string, so let's set that up. 35 00:02:12,574 --> 00:02:13,665 Return, have a string. 36 00:02:13,665 --> 00:02:19,483 It needs to contain the name instance attribute. 37 00:02:19,483 --> 00:02:22,758 So you would get something like Tim is learning to code, 38 00:02:22,758 --> 00:02:25,694 where Tim is the name being passed into our class. 39 00:02:25,694 --> 00:02:27,832 So I'm gonna copy our string here actually. 40 00:02:27,832 --> 00:02:30,294 We'll just replace this. 41 00:02:30,294 --> 00:02:35,505 Okay, and Tim, I forgot, 42 00:02:35,505 --> 00:02:40,913 it's going to give us the funky. 43 00:02:40,913 --> 00:02:45,431 If you notice that, it's kind of hard to tell, it's the same thing up here, 44 00:02:45,431 --> 00:02:47,586 I'm gonna have to change those, but 45 00:02:47,586 --> 00:02:50,661 it's actually a different type of quotation mark. 46 00:02:50,661 --> 00:02:53,397 That happens sometimes when you copy and 47 00:02:53,397 --> 00:02:56,703 paste from like a Word doc into your coding file. 48 00:02:56,703 --> 00:03:01,088 So you can see when I change it to what I usually use for double quotes, 49 00:03:01,088 --> 00:03:03,140 it shows something different. 50 00:03:03,140 --> 00:03:07,830 So just as a heads up, make sure you have your right quotations in there. 51 00:03:08,910 --> 00:03:11,280 Okay, with that fixed, now we can get back to it. 52 00:03:11,280 --> 00:03:17,023 So I'm gonna turn this into an f string because we need to add this variable, 53 00:03:17,023 --> 00:03:20,369 our instance attribute, into our string. 54 00:03:20,369 --> 00:03:23,906 So that when we create a class and we pass in a name, 55 00:03:23,906 --> 00:03:29,149 that is what will be passed into this method and printed out to the screen. 56 00:03:29,149 --> 00:03:31,576 So we need our brackets. 57 00:03:31,576 --> 00:03:37,050 And then to access our instance attribute, we do self dot, 58 00:03:37,050 --> 00:03:42,865 and you can see up there at the top, it's already got it for us. 59 00:03:42,865 --> 00:03:48,020 So I can hit Enter, name, and you can see it says variable name, perfect. 60 00:03:50,090 --> 00:03:54,035 Now number six, create an instance of the class and 61 00:03:54,035 --> 00:03:58,250 save it to a variable named my_treehouse. 62 00:03:58,250 --> 00:04:00,680 Pass in your name as the name parameter. 63 00:04:02,000 --> 00:04:07,260 All right, so let me get out of our class, give a little space here. 64 00:04:07,260 --> 00:04:13,182 Variable needs to be called my_treehouse, set it equal to an instance of the class. 65 00:04:13,182 --> 00:04:16,408 So we're gonna call the class just like we would call a function. 66 00:04:16,408 --> 00:04:19,156 And then you can see it's looking for a name, 67 00:04:19,156 --> 00:04:22,204 it's giving us a hint here in Visual Studio Code. 68 00:04:22,204 --> 00:04:25,576 Because I need to pass in that name variable, 69 00:04:25,576 --> 00:04:30,386 it's going to look for it because it's required, right here. 70 00:04:30,386 --> 00:04:34,615 So I'm gonna pass in my name, which is Megan. 71 00:04:34,615 --> 00:04:40,330 And then our final step, call your learn method and print out the result. 72 00:04:42,260 --> 00:04:47,258 So calling our method, we need to do, I'm just gonna use our 73 00:04:47,258 --> 00:04:52,850 variable because that is equal to our instance of our class. 74 00:04:52,850 --> 00:04:58,957 And then to call our method, .learn, and you can see it pops it up. 75 00:04:58,957 --> 00:05:00,871 Remember, learn is our method, and 76 00:05:00,871 --> 00:05:04,906 it even tells you right here when you highlight over it in Visual Studio Code. 77 00:05:04,906 --> 00:05:09,619 And then it automatically popped in the parentheses for us because it's a function 78 00:05:09,619 --> 00:05:14,120 that we're going to call, a method is a function inside of a class. 79 00:05:14,120 --> 00:05:15,906 Now, this is going to return something, 80 00:05:15,906 --> 00:05:18,540 which is why I'm asking you to print it out. 81 00:05:18,540 --> 00:05:21,333 So I'm gonna wrap this in a print statement, 82 00:05:23,771 --> 00:05:29,391 Hit Save, and now I can run my file. 83 00:05:29,391 --> 00:05:33,521 So python, I'm on a Mac, so I have to do 3 cuz I have a couple different 84 00:05:33,521 --> 00:05:35,628 versions of Python on my computer. 85 00:05:35,628 --> 00:05:39,802 And then the file name, which is oop_practice.py. 86 00:05:39,802 --> 00:05:43,722 Hit Enter, and you should get your string. 87 00:05:43,722 --> 00:05:50,961 So I pass in Megan as my name, which is set as my instance attribute self.name, 88 00:05:50,961 --> 00:05:57,239 which is being used inside of this method which returns this string. 89 00:05:57,239 --> 00:06:02,574 And we're calling that method down here and printing it all out, 90 00:06:02,574 --> 00:06:07,918 which is getting sent to our console, Megan is learning to code. 91 00:06:07,918 --> 00:06:13,450 I hope you enjoyed the OOP vocabulary practice session here. 92 00:06:13,450 --> 00:06:19,210 Keep practicing, keep working on your OOP, and it'll definitely start to stick.