1 00:00:00,848 --> 00:00:07,710 In object-oriented Python, a function inside of a class is called a method. 2 00:00:07,710 --> 00:00:12,430 In this video, we're going to dive into how to add methods to a class, 3 00:00:12,430 --> 00:00:15,460 pass in arguments and call a method. 4 00:00:15,460 --> 00:00:18,190 Let's start by adding a method to this car class. 5 00:00:19,350 --> 00:00:20,974 It will be called stop 6 00:00:31,816 --> 00:00:34,730 The format is the same as a regular function, but 7 00:00:34,730 --> 00:00:37,870 it needs to take self as an argument. 8 00:00:37,870 --> 00:00:42,870 All methods inside of a class will need to take self, otherwise it'll throw an error. 9 00:00:44,150 --> 00:00:47,677 Inside of our method, let's just print out "The car has stopped". 10 00:00:54,556 --> 00:00:57,660 Let's jump down here and call our method. 11 00:00:59,030 --> 00:01:02,450 In order to call it, we will need our instance and 12 00:01:02,450 --> 00:01:04,878 dot notation plus the name of the function. 13 00:01:04,878 --> 00:01:12,010 So, car_one.stop(). 14 00:01:12,010 --> 00:01:12,700 Run the file. 15 00:01:15,800 --> 00:01:18,560 And voila, it printed out our message. 16 00:01:21,060 --> 00:01:25,810 Now, what if we needed to pass in information to our method? 17 00:01:25,810 --> 00:01:26,590 How do we do that? 18 00:01:27,680 --> 00:01:29,720 Well, it works just like a regular function. 19 00:01:30,780 --> 00:01:32,936 Let's create a new method called go. 20 00:01:41,250 --> 00:01:43,980 It'll take self and a speed argument. 21 00:01:46,040 --> 00:01:51,021 And then print out an f string, "The car is going" and pass in the speed. 22 00:02:01,914 --> 00:02:07,490 Cool, now let's call this method a few times and pass in a few different speeds. 23 00:02:07,490 --> 00:02:10,738 Go ahead and try it on your own, pass in whatever speeds you like. 24 00:02:30,197 --> 00:02:33,703 Okay, so we've tackled creating a method, 25 00:02:33,703 --> 00:02:38,435 calling a method and passing an argument so far. 26 00:02:38,435 --> 00:02:40,285 It's time for you to give it a go on your own. 27 00:02:41,420 --> 00:02:45,990 Create a class, add a method that takes at least one argument. 28 00:02:45,990 --> 00:02:50,530 Give it some logic or a simple print statement and then call it. 29 00:02:50,530 --> 00:02:54,590 If you get stuck, rewind me and rewatch this video. 30 00:02:54,590 --> 00:02:57,840 It's okay if it takes you a few times for things to click. 31 00:02:57,840 --> 00:02:59,203 I'll see you back here in a bit. 32 00:03:03,354 --> 00:03:04,560 Back? 33 00:03:04,560 --> 00:03:06,060 Let's keep going. 34 00:03:06,060 --> 00:03:10,100 A powerful thing about classes is we can use our attributes inside of 35 00:03:10,100 --> 00:03:10,980 any method we have. 36 00:03:12,090 --> 00:03:16,887 Let's add a new instance attribute called is moving and set it to false. 37 00:03:25,180 --> 00:03:28,630 Instance attributes don't always have to be passed in. 38 00:03:30,660 --> 00:03:32,950 Now we can adjust our stop and 39 00:03:32,950 --> 00:03:38,170 go methods to check this attribute first before printing a message. 40 00:03:38,170 --> 00:03:42,750 That way we don't tell a stopped car to stop more cuz 41 00:03:42,750 --> 00:03:44,610 that wouldn't really make any sense. 42 00:03:46,200 --> 00:03:49,345 In our stop method, let's check if the car is moving. 43 00:03:57,375 --> 00:04:00,780 If it is, let's print out that the car has stopped 44 00:04:03,322 --> 00:04:07,157 And then set our is moving attribute to false 45 00:04:12,956 --> 00:04:17,081 Otherwise, let's let them know the car has already stopped. 46 00:04:33,763 --> 00:04:38,390 Inside of our go method, let's do something similar. 47 00:04:38,390 --> 00:04:45,968 If is moving is false, which means we need to check for, not is moving. 48 00:04:48,605 --> 00:04:52,268 Then let's let the user know the car has started moving and 49 00:04:52,268 --> 00:04:54,003 set the attribute to true. 50 00:05:09,301 --> 00:05:13,047 We can print out the car's speed outside of the statement, so 51 00:05:13,047 --> 00:05:16,600 the speed can be changed if the user wants. 52 00:05:16,600 --> 00:05:17,100 Nice. 53 00:05:18,670 --> 00:05:22,854 Let's call both methods a few times to see our work in action. 54 00:05:36,834 --> 00:05:37,770 Run the file. 55 00:05:39,930 --> 00:05:43,630 Take a minute to follow the logic of what's happening in the console 56 00:05:45,110 --> 00:05:46,460 with the code you just wrote. 57 00:05:47,840 --> 00:05:55,450 For example, when is instance is created, is moving is set to false. 58 00:05:56,690 --> 00:06:02,184 So when stop is called, we get the 59 00:06:02,184 --> 00:06:04,940 "The car is all ready stopped" message. 60 00:06:06,930 --> 00:06:12,120 When go is called, is_moving is set to True and 61 00:06:12,120 --> 00:06:17,010 we get "The car starts moving" and our speed message. 62 00:06:18,740 --> 00:06:22,050 Keep following the rest of the logic on your own. 63 00:06:22,050 --> 00:06:25,350 This helps you make sure you know what is happening with your code. 64 00:06:25,350 --> 00:06:27,007 Go ahead and pause me. 65 00:06:29,671 --> 00:06:31,140 Nice work. 66 00:06:31,140 --> 00:06:33,580 One last challenge to tackle. 67 00:06:33,580 --> 00:06:37,130 If the car is moving, then it's using up gas. 68 00:06:38,310 --> 00:06:41,456 Let's create a new instance attribute called gas. 69 00:06:46,773 --> 00:06:49,984 Now let's create a method called use_gas. 70 00:07:04,683 --> 00:07:11,993 Inside of the method, reduce the gas value by 50, to make it easier to test. 71 00:07:18,543 --> 00:07:26,104 Then return False if the self.gas value is less than or equal to zero. 72 00:07:31,288 --> 00:07:33,750 And True, if there's still gas. 73 00:07:38,891 --> 00:07:42,470 Now we can use this method inside our go method. 74 00:07:43,880 --> 00:07:45,480 If the car is moving, 75 00:07:45,480 --> 00:07:51,330 then we need to call our use_gas method to reduce the amount of gas we have. 76 00:07:51,330 --> 00:07:54,640 If we are out of gas then the car cannot move at all. 77 00:07:56,120 --> 00:07:58,081 At the start of this method, 78 00:07:58,081 --> 00:08:03,493 let's check if we have gas by calling the method using self in an if statement. 79 00:08:08,787 --> 00:08:11,173 This both calls the method and 80 00:08:11,173 --> 00:08:15,090 lets us know if we still have gas at the same time. 81 00:08:16,170 --> 00:08:20,221 If we have gas, then we can run the logic we already had. 82 00:08:24,247 --> 00:08:29,566 Otherwise, we need to let the user know they've run out of gas and 83 00:08:29,566 --> 00:08:32,773 call the stop function to stop the car. 84 00:08:45,600 --> 00:08:49,130 Go ahead and run the file to see what's changed in the console. 85 00:08:54,390 --> 00:08:58,721 Take a minute again to think through what's happening in the console and 86 00:08:58,721 --> 00:09:00,570 follow the logic in your code. 87 00:09:01,580 --> 00:09:04,755 When you feel comfortable that you understand what's happening, 88 00:09:04,755 --> 00:09:05,595 then unpause me. 89 00:09:08,603 --> 00:09:11,180 It's recap o'clock. 90 00:09:11,180 --> 00:09:12,150 In this video, 91 00:09:12,150 --> 00:09:16,630 you've learned how to add functions to your class, which are called methods. 92 00:09:17,690 --> 00:09:21,580 They follow the same syntax as regular functions, but 93 00:09:21,580 --> 00:09:26,610 are placed inside of a class and take self as an argument. 94 00:09:27,980 --> 00:09:33,740 You can pass in values to a method, call other methods 95 00:09:33,740 --> 00:09:39,255 inside of one and access attributes of your class. 96 00:09:44,230 --> 00:09:50,010 When calling a method, you use dot notation on an instance of the class. 97 00:09:50,010 --> 00:09:53,375 Don't forget to pass in values if the method calls for them. 98 00:09:55,262 --> 00:09:58,900 Try out what you've learned so far on your own. 99 00:09:58,900 --> 00:10:03,990 Create your own class, add attributes, methods and play around. 100 00:10:03,990 --> 00:10:07,270 This is how you learn and solidify your new knowledge. 101 00:10:08,480 --> 00:10:11,430 Also, try helping someone else. 102 00:10:11,430 --> 00:10:14,540 Nothing helps us realize how much we know or 103 00:10:14,540 --> 00:10:17,010 don't know, by helping someone else learn. 104 00:10:18,310 --> 00:10:22,410 Lastly, never feel bad if you need to rewatch a video. 105 00:10:22,410 --> 00:10:27,010 Some concepts may be harder than others and there's no judgment on how many 106 00:10:27,010 --> 00:10:31,870 times you needed to watch a video before the concept finally clicked. 107 00:10:31,870 --> 00:10:35,570 Take as long as you need and reach out if you hit a roadblock. 108 00:10:35,570 --> 00:10:36,200 We're here to help.