Let's Make a Class!4:21 with Kenneth Love
With vocabulary out of the way, let's see how to define our own classes.
We'll be doing a lot of importing in this course because it gives us the best way of organizing our code into logical bunches. It's a good practice to get into!
Notice that we can immediately read and write our class attributes! We don't usually write getters or setters (methods for retrieving or updating data on an instance) in Python.
Now we can base our class on Python's built in classes. 0:00 But that's a complicated thing to start on. 0:03 So instead let's start by creating brand new classes from our imagination. 0:05 Let's create a class that would represent a character in a role-playing game. 0:10 I like to play a sneaky thief or assassin characters. 0:13 So I'm gonna make one of those. 0:16 If you wanna make a wizard or a warrior, go right ahead. 0:18 So I'm here in characters.py. 0:20 And I'm gonna type in class Thief. 0:22 And just like that I have a class of my very own. 0:25 Well, okay, it won't compile but it's a class. 0:29 All classes in Python start with the keyword class. 0:32 This is just like when you start a function. 0:35 You have to use the def keyword. 0:37 Then again like a function next comes the classes name. 0:39 By convention classes always have to start with a capital letter. 0:43 If they have multiple words in them. 0:47 Like let's say we were doing class MeleeW eapon. 0:48 Then each word is capitalized. 0:54 Now I don't need that class so I'm gonna go ahead and delete it. 0:56 So to be a valid class, it has to have something in it. 0:59 The code block, right? 1:02 Got the colon and everything. 1:03 So let me put pass in here. 1:05 And then I can test it out on the console. 1:07 So I'm gonna go ahead and save and then I'll come down to my console. 1:09 And now if I go into Python. 1:13 I can import my class and I can use it. 1:16 So I'll say from characters import thief. 1:19 And then I will say that Kenneth is a thief. 1:23 So did I just called a class like a function? 1:27 Well you bet I did. 1:30 But this didn't run the class. 1:31 Not like it runs a function. 1:33 Classes don't run. 1:34 What this did though, is it created an instance of the class. 1:36 So now if I look at my variable, I'll see some information about it. 1:40 So check that out, yeah. 1:43 It's an object. 1:45 Specifically, it's an object from character.thief, 1:46 which is the class name, right? 1:49 So we have object, some characters .thief. 1:51 So cool. 1:54 That's handy information. 1:55 And this thing out here, the 0x7f blah, blah, yours will be different. 1:56 That tells me what spot in memory my instance takes out. 2:01 Okay, so what about attributes? 2:05 How can I give my class some information? 2:07 Well, I can define attributes right inside the class, 2:11 just like I would define variables. 2:15 So, I'm a thief, I'm probably sneaky, alright? 2:18 Let's add that, so we'll say sneaky = True. 2:22 And we'll save that. 2:25 And now I'm gonna go back down here to my console. 2:26 You may find yourself going back and forth a whole lot. 2:29 Simply because it's easier to see what's going on. 2:31 As you make incremental little changes. 2:34 So from characters import Thief. 2:36 And, kenneth = Thief(). 2:38 And then if I say. 2:42 And I want to see what the sneaky attribute has in it. 2:44 I have to access that attribute. 2:47 Python uses dot syntax to access members of a class or an instance. 2:49 And a member is anything that's defined on the class. 2:53 So attributes, methods, that whole kind of thing. 2:56 Right? 2:59 Well I know the class defined a sneaky attribute. 3:00 So I should be able to get to that with a dot right? 3:03 Kenneth.sneaky and I get back true. 3:05 So cool. 3:09 Now I can also get that on the class. 3:11 If I do thief.sneaky. 3:14 I get that's true as well. 3:16 Remember though that instances are responsible for 3:18 their own attribute values. 3:20 So I should be able to change the sneaky attribute just on my instance. 3:22 So I'll do kenneth.sneaky = False. 3:27 And now if I look at kenneth.sneaky I get back False, 3:30 not a very good thief then, I guess. 3:34 But did that change the class? 3:36 No, the class is still sneaky. 3:39 Now, that's not always true the other way around. 3:41 I'd like you to play with that and see what you can find out there. 3:44 But I change my own sneakiness. 3:47 And that won't affect any other thief instances that I create. 3:49 Like I said, there is a bit of a gotcha without read and classes. 3:53 You're not quite ready for it though. 3:57 We'll come back to that in a little while. 3:59 So long as you stick the changing attribute values on instances you'll 4:01 be fine. 4:04 We can of course delete instances of our class. 4:05 Using the del key word. 4:08 And now if I try to look at kenneth. 4:10 I get a name error. 4:11 Cuz it is not defined. 4:12 All right. I think you are ready for 4:14 a code challenge about attributes. 4:15 Get that done. 4:16 And then come back. 4:17 And we'll give your classes some actions with methods. 4:18
You need to sign up for Treehouse in order to download course files.Sign up