Methods5:48 with Kenneth Love
We can give our class some special abilities by defining methods for it.
You don't have to call the instance argument
self if you don't want to. It's not a required name but it's a very common convention (I don't think I've ever seen code that didn't).
class Thief: sneaky = True def pickpocket(instance): if instance.sneaky: return bool(random.randint(0, 1)) return False
If all we could do with classes is set and 0:00 use attributes, they wouldn't be all that useful, would they? 0:02 Like I mentioned before, though, 0:04 we can give verbs to our objects by creating methods. 0:05 Now, don't get too worried about methods, cuz I'll let you in on a secret. 0:08 Methods are just functions that belong to a class. 0:11 So, if you can write a function, you can write a method. 0:13 They do have one little gotcha, though. 0:16 So, let's get into Workspaces so I can teach you all about them. 0:17 So, I'm gonna simplify things a little bit, 0:20 I'm gonna leave out a couple of things here at first. 0:22 Methods in classes are the actions of the instances of your class can do. 0:24 For example, my Thief class might have a pickpocket method that lets it try and 0:29 snatch something out of another person's pocket. 0:33 Methods belong to the class, so they're indented inside of it, 0:36 just like attributes are. 0:39 So, we'll say def pickpocket and that'll take an argument self and 0:40 then we'll return a bool of random.randit between 0 and 1. 0:41 And that means I 0:41 need to import random. 0:46 So, what's this self thing that's going on here? 0:57 What's that about? 1:01 Well, methods are functions that's belong to a class. 1:02 Whenever they're used, 1:05 however, they're used by an instance, not by the actual class. 1:06 Because of that, methods always take at least one parameter, 1:11 which represents the instance that's using the method. 1:14 Now, by convention that parameter's always called self, but 1:17 you can call it anything you want in your own classes. 1:21 But I highly recommend you stick with self. 1:23 Now, the neat thing here is you don't give any arguments to self 1:26 when you use the method. 1:29 Let's try this out down here in our console. 1:31 So, Python from characters import Thief. 1:33 And kenneth = Thief just like before, right? 1:37 So now, I can do kenneth.pickpocket. 1:43 And just like any other function, I have to call it with parenthesis. 1:47 And since the method doesn't take any arguments other than self I don't put 1:50 anything inside of here. 1:55 And I get back False, so it randomly picked false. 1:57 So, cool. 2:01 Now, I don't usually like to show you clunky ways of achieving something but 2:02 I feel like this whole self thing needs a little bit more explanation. 2:06 Now, let's do the same method but 2:10 we're going to do it in a slightly different way, all right? 2:12 So, if I tried to Thief.pickpocket. 2:15 Then I get an error. 2:19 I get this TypeError saying that pickpocket is missing a required 2:20 positional argument, self. 2:23 Okay, so let's give it a self, pickpocket, and 2:26 I'm going to give it an instance of the class. 2:29 And this time I get False again. 2:33 So, we just ran the exact same code, but 2:35 this time we had to provide an argument to pickpocket or it failed, right? 2:37 Since we called it directly from the class, 2:41 we had to provide an instance that it would be used by. 2:42 So, let's add a little something to the class to make this instance use 2:46 more obvious. 2:49 I' m gonna go ahead and exit and them I'm gonna clear this screen. 2:50 I'm gonna hop back up here to this. 2:54 Okay, so inside pickpocket let's add a new line here, 2:55 print Called by, and then let's fill that in with self. 3:00 So, now our instance will print out who it is that's trying to pickpocket someone 3:05 else, okay? 3:10 Maybe not the best approach for a thief but 3:11 we're here to learn not to steal things from people. 3:13 So, from characters import Thief, Kenneth = Thief. 3:17 And now, let's do kenneth.pickpocket. 3:23 Okay, called by characters.Thief object at that address, and True. 3:27 Cool, that time I succeeded, all right? 3:31 So now, let's try our other method of it, Thief.pickpocket. 3:34 And we'll pass in the instance, which is kenneth. 3:38 And I get the same output, the exact same output, right? 3:42 Called by characters.thief object at 0x7f99b71edef0, all right? 3:45 Exactly same memory address. 3:50 So, the fact that we have the exact same memory address shows us that it's 3:54 using the exact same object for calling the method both times. 3:58 This self concept often trips up early programmers, but 4:03 practice makes perfect on it. 4:07 And thankfully, Python will gripe at you loudly if you forget to use self as 4:08 a parameter if you forget to call it on a method. 4:12 All right, one more thing about self. 4:17 We can always use self to talk about the current instance, like in the method. 4:19 So, let's check and 4:25 make sure that our thief is sneaky before we even attempt to pickpocket somebody. 4:27 So, let's take out this print line. 4:32 Cuz we don't need that. 4:35 And let's say if self.sneaky, and then we'll indent that. 4:36 Otherwise, we'll return False, okay? 4:41 So, if the instance's sneaky attribute is set to True, 4:44 then we'll try and pickpocket. 4:49 Otherwise, we just always return a False. 4:51 All right, so that's cool. 4:53 Now let's give that a try. 4:55 Come back down here. 4:57 From characters import thief. 5:01 kenneth = thief. 5:03 And let's try this. 5:07 So, kenneth.pickpocket. 5:08 And we get false, and if I run this another time or two I get, 5:10 yeah there we go, True, False, whatever. 5:13 Okay, so now let's make sure that I am not sneaky, all right? 5:16 I'm a clumsy non sneaky thief. 5:21 And now, let's try kenneth.pickpocket. 5:24 False, False, False, False, False, all right? 5:28 So, it's always False. 5:30 I'm gonna get caught cuz I'm not sneaky. 5:32 So, inside of a class's method, self always refers to the instance. 5:36 And I can use that to get to the instance's attributes and 5:40 even other methods. 5:43 Let's try this whole method thing out with a co-challenge or two. 5:45
You need to sign up for Treehouse in order to download course files.Sign up