1 00:00:00,270 --> 00:00:02,090 If all we could do with classes is set and 2 00:00:02,090 --> 00:00:04,720 use attributes, they wouldn't be all that useful, would they? 3 00:00:04,720 --> 00:00:05,730 Like I mentioned before, though, 4 00:00:05,730 --> 00:00:08,500 we can give verbs to our objects by creating methods. 5 00:00:08,500 --> 00:00:11,390 Now, don't get too worried about methods, cuz I'll let you in on a secret. 6 00:00:11,390 --> 00:00:13,790 Methods are just functions that belong to a class. 7 00:00:13,790 --> 00:00:16,030 So, if you can write a function, you can write a method. 8 00:00:16,030 --> 00:00:17,700 They do have one little gotcha, though. 9 00:00:17,700 --> 00:00:20,690 So, let's get into Workspaces so I can teach you all about them. 10 00:00:20,690 --> 00:00:22,527 So, I'm gonna simplify things a little bit, 11 00:00:22,527 --> 00:00:24,658 I'm gonna leave out a couple of things here at first. 12 00:00:24,658 --> 00:00:29,219 Methods in classes are the actions of the instances of your class can do. 13 00:00:29,219 --> 00:00:33,519 For example, my Thief class might have a pickpocket method that lets it try and 14 00:00:33,519 --> 00:00:36,400 snatch something out of another person's pocket. 15 00:00:36,400 --> 00:00:39,260 Methods belong to the class, so they're indented inside of it, 16 00:00:39,260 --> 00:00:40,458 just like attributes are. 17 00:00:40,458 --> 00:00:41,024 So, we'll say def pickpocket and that'll take an argument self and 18 00:00:41,024 --> 00:00:41,555 then we'll return a bool of random.randit between 0 and 1. 19 00:00:41,555 --> 00:00:46,901 And that means I 20 00:00:46,901 --> 00:00:55,130 need to import random. 21 00:00:57,290 --> 00:01:01,560 So, what's this self thing that's going on here? 22 00:01:01,560 --> 00:01:02,955 What's that about? 23 00:01:02,955 --> 00:01:05,780 Well, methods are functions that's belong to a class. 24 00:01:05,780 --> 00:01:06,630 Whenever they're used, 25 00:01:06,630 --> 00:01:11,410 however, they're used by an instance, not by the actual class. 26 00:01:11,410 --> 00:01:14,520 Because of that, methods always take at least one parameter, 27 00:01:14,520 --> 00:01:17,610 which represents the instance that's using the method. 28 00:01:17,610 --> 00:01:21,310 Now, by convention that parameter's always called self, but 29 00:01:21,310 --> 00:01:23,860 you can call it anything you want in your own classes. 30 00:01:23,860 --> 00:01:26,370 But I highly recommend you stick with self. 31 00:01:26,370 --> 00:01:29,470 Now, the neat thing here is you don't give any arguments to self 32 00:01:29,470 --> 00:01:31,230 when you use the method. 33 00:01:31,230 --> 00:01:33,476 Let's try this out down here in our console. 34 00:01:33,476 --> 00:01:37,872 So, Python from characters import Thief. 35 00:01:37,872 --> 00:01:43,060 And kenneth = Thief just like before, right? 36 00:01:43,060 --> 00:01:47,500 So now, I can do kenneth.pickpocket(). 37 00:01:47,500 --> 00:01:50,740 And just like any other function, I have to call it with parenthesis. 38 00:01:50,740 --> 00:01:55,003 And since the method doesn't take any arguments other than self I don't put 39 00:01:55,003 --> 00:01:56,480 anything inside of here. 40 00:01:57,540 --> 00:02:00,670 And I get back False, so it randomly picked false. 41 00:02:01,900 --> 00:02:02,790 So, cool. 42 00:02:02,790 --> 00:02:06,610 Now, I don't usually like to show you clunky ways of achieving something but 43 00:02:06,610 --> 00:02:10,540 I feel like this whole self thing needs a little bit more explanation. 44 00:02:10,540 --> 00:02:12,156 Now, let's do the same method but 45 00:02:12,156 --> 00:02:15,074 we're going to do it in a slightly different way, all right? 46 00:02:15,074 --> 00:02:17,734 So, if I tried to Thief.pickpocket(). 47 00:02:19,050 --> 00:02:20,330 Then I get an error. 48 00:02:20,330 --> 00:02:23,310 I get this TypeError saying that pickpocket is missing a required 49 00:02:23,310 --> 00:02:24,780 positional argument, self. 50 00:02:26,080 --> 00:02:29,950 Okay, so let's give it a self, pickpocket, and 51 00:02:29,950 --> 00:02:32,140 I'm going to give it an instance of the class. 52 00:02:33,520 --> 00:02:35,190 And this time I get False again. 53 00:02:35,190 --> 00:02:37,350 So, we just ran the exact same code, but 54 00:02:37,350 --> 00:02:41,028 this time we had to provide an argument to pickpocket or it failed, right? 55 00:02:41,028 --> 00:02:42,450 Since we called it directly from the class, 56 00:02:42,450 --> 00:02:45,090 we had to provide an instance that it would be used by. 57 00:02:46,090 --> 00:02:49,360 So, let's add a little something to the class to make this instance use 58 00:02:49,360 --> 00:02:50,540 more obvious. 59 00:02:50,540 --> 00:02:52,840 I' m gonna go ahead and exit and them I'm gonna clear this screen. 60 00:02:54,180 --> 00:02:55,979 I'm gonna hop back up here to this. 61 00:02:55,979 --> 00:03:00,243 Okay, so inside pickpocket let's add a new line here, 62 00:03:00,243 --> 00:03:04,610 print Called by, and then let's fill that in with self. 63 00:03:05,850 --> 00:03:10,424 So, now our instance will print out who it is that's trying to pickpocket someone 64 00:03:10,424 --> 00:03:11,230 else, okay? 65 00:03:11,230 --> 00:03:13,995 Maybe not the best approach for a thief but 66 00:03:13,995 --> 00:03:17,553 we're here to learn not to steal things from people. 67 00:03:17,553 --> 00:03:23,670 So, from characters import Thief, kenneth = Thief. 68 00:03:23,670 --> 00:03:27,690 And now, let's do kenneth.pickpocket. 69 00:03:27,690 --> 00:03:31,640 Okay, called by characters.Thief object at that address, and True. 70 00:03:31,640 --> 00:03:34,300 Cool, that time I succeeded, all right? 71 00:03:34,300 --> 00:03:38,826 So now, let's try our other method of it, Thief.pickpocket(). 72 00:03:38,826 --> 00:03:41,160 And we'll pass in the instance, which is kenneth. 73 00:03:42,340 --> 00:03:45,542 And I get the same output, the exact same output, right? 74 00:03:45,542 --> 00:03:50,140 Called by characters.Thief object at 0x7f99b71edef0, all right? 75 00:03:50,140 --> 00:03:54,040 Exactly same memory address. 76 00:03:54,040 --> 00:03:58,922 So, the fact that we have the exact same memory address shows us that it's 77 00:03:58,922 --> 00:04:03,930 using the exact same object for calling the method both times. 78 00:04:03,930 --> 00:04:07,120 This self concept often trips up early programmers, but 79 00:04:07,120 --> 00:04:08,730 practice makes perfect on it. 80 00:04:08,730 --> 00:04:12,170 And thankfully, Python will gripe at you loudly if you forget to use self as 81 00:04:12,170 --> 00:04:16,310 a parameter if you forget to call it on a method. 82 00:04:17,430 --> 00:04:19,410 All right, one more thing about self. 83 00:04:19,410 --> 00:04:24,640 We can always use self to talk about the current instance, like in the method. 84 00:04:25,730 --> 00:04:27,190 So, let's check and 85 00:04:27,190 --> 00:04:32,801 make sure that our thief is sneaky before we even attempt to pickpocket somebody. 86 00:04:32,801 --> 00:04:35,306 So, let's take out this print line. 87 00:04:35,306 --> 00:04:36,890 Cuz we don't need that. 88 00:04:36,890 --> 00:04:41,630 And let's say if self.sneaky, and then we'll indent that. 89 00:04:41,630 --> 00:04:44,750 Otherwise, we'll return False, okay? 90 00:04:44,750 --> 00:04:49,540 So, if the instance's sneaky attribute is set to True, 91 00:04:49,540 --> 00:04:51,210 then we'll try and pickpocket. 92 00:04:51,210 --> 00:04:53,650 Otherwise, we just always return a False. 93 00:04:53,650 --> 00:04:54,510 All right, so that's cool. 94 00:04:55,560 --> 00:04:57,979 Now let's give that a try. 95 00:04:57,979 --> 00:04:58,930 Come back down here. 96 00:05:01,628 --> 00:05:03,354 From characters import Thief. 97 00:05:03,354 --> 00:05:07,360 kenneth = Thief(). 98 00:05:07,360 --> 00:05:08,500 And let's try this. 99 00:05:08,500 --> 00:05:10,060 So, kenneth.pickpocket(). 100 00:05:10,060 --> 00:05:13,841 And we get false, and if I run this another time or two I get, 101 00:05:13,841 --> 00:05:16,960 yeah there we go, True, False, whatever. 102 00:05:16,960 --> 00:05:21,749 Okay, so now let's make sure that I am not sneaky, all right? 103 00:05:21,749 --> 00:05:24,927 I'm a clumsy non sneaky thief. 104 00:05:24,927 --> 00:05:28,134 And now, let's try kenneth.pickpocket(). 105 00:05:28,134 --> 00:05:30,822 False, False, False, False, False, all right? 106 00:05:30,822 --> 00:05:32,838 So, it's always False. 107 00:05:32,838 --> 00:05:36,690 I'm gonna get caught cuz I'm not sneaky. 108 00:05:36,690 --> 00:05:40,011 So, inside of a class's method, self always refers to the instance. 109 00:05:40,011 --> 00:05:43,599 And I can use that to get to the instance's attributes and 110 00:05:43,599 --> 00:05:45,600 even other methods. 111 00:05:45,600 --> 00:05:49,020 Let's try this whole method thing out with a co-challenge or two.