Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Controlling Conversion6:24 with Kenneth Love
Whether we like it or not, someone, somewhere, is going to turn an instance of our class into a string. Or an int. Let's make sure it happens like we want.
You can read more about the magic methods. There are a lot of them, though, so don't try and memorize them all. You'll find that, over time, you memorize the most useful ones. Here are the ones I recommend you start with:
__str__- Control how your instances turn into strings
__init__- Customize the initialization of your instances
[MUSIC] 0:00 Now that we have a handle on creating classes, let's talk about one of the most 0:04 powerful features of Python's classes, magic methods. 0:08 You've already seen one magic method, the __init__ one, 0:11 remember we call it magic because it's a method that Python will call for you. 0:14 As you can probably imagine, there are tons of Magic Methods that Python uses 0:18 behind the scenes to make things run smoothly. 0:21 You'll be learning about some of them, but 0:24 I've linked to the full documentation about them in the teacher's notes. 0:25 I really like Magic Methods, because despite their name, 0:29 they let us peel back some of the magic in Python and see how things are working. 0:32 One of the things that happens behind the scenes, is conversion between types. 0:35 Think about when you use the STR function on an integer. 0:39 What happens? 0:42 It's actually controlled by a Magic Method. 0:43 Let's go to WorkSpaces and check it out. 0:45 Let's start with our illustrious thief class. 0:47 Right now if we turn a thief into a string, 0:50 we get something that's well, less than useful. 0:54 Not really that great. 0:58 So remember that the 0x blah, blah, 1:00 blah bit refers to where the object is stored in memory. 1:03 Handy but not really handy. 1:07 Wouldn't it be easier to have it tell us the name of the character? 1:11 Well, let's make it do that then. 1:14 I think this is probably something we'd want all characters to have though, 1:16 so we'll do it on the character class and not on the thief class. 1:20 You'll often find the implement Magic Methods on your base classes, but 1:25 not on your specific implementation classes. 1:29 Those that you use directly, 1:32 usually only have very specific code that they need to do their jobs. 1:34 The more generic code goes into the parent classes further up the chain. 1:37 So let's add a new method here. 1:42 And we'll call it __str__. 1:47 As you probably could have guessed. 1:51 So we actually don't need that line. 1:55 So this method should return a string that you want to use to 1:57 identify your object, whenever it's turned into a string. 2:03 So in this case, let's return. 2:07 Let's return two things. 2:12 Let's return the self.__class__.__name__, so we can see if it's a thief, 2:13 or an archer, or a barbarian, or whatever and let's return self.name. 2:19 So now, if we recheck what happens when we run play.py. 2:24 We see Thief Kenneth, so that's awesome, that's very cool, that's much, 2:31 much friendlier, and it's better output. 2:35 A related method that's often on a written is dander repr, dander repr 2:38 which is used to give the official string representation of an instance. 2:43 Now this was mostly used for debugging and 2:48 should present as much useful information as possible. 2:50 Right now, our rpg gives back what our old string looked like, and 2:53 I think that's good output for it, so I'm gonna leave that alone. 2:57 So that's converting to a string, but what about other conversions? 3:00 More complex conversions, like turning an instance into a dictionary or 3:04 other object while possible, are beyond the scope of this course. 3:07 We can though easily set up converting our objects into floats. 3:11 Now I don't think any of our existing classes would work well for this, so 3:16 let's make a new one. 3:19 And we'll do this out here in the OO Python directory not in the rpg directory. 3:20 Now I'm going to call this numstring.py. 3:25 Let's make a class that holds a number, holds an int or a float, or 3:29 whatever as a string. 3:33 But, it'll let us turn it into an int whenever we want to. 3:36 Let's just do this, so we'll say class NumStr, cuz that's a catchy name. 3:40 And then def __init__ (self and value). 3:47 And self.value = str (value). 3:51 And then we'll define the __str, and we'll return self.value. 3:57 And we'll define __int, and we'll return the int(self.value). 4:04 And let's also define __float, which returns the float(self.value). 4:13 Now, I'm sure you could guess what __int and __float did. 4:20 Even before I told you. 4:25 Since you've seen __str. 4:26 But it's still good to see them. 4:27 Hopefully, you can see why you probably wouldn't want to use this as real code. 4:31 It's good for experimentation though. 4:36 You should always play around, and 4:37 try just whatever silly ideas you can come up with. 4:39 So, in the interest of being silly, let’s try it out. 4:43 Let’s come down here, and then go into python. 4:47 And I’m going to say from NumString import Numstring. 4:50 And I’m going to say five = Numstring (5). 4:56 And if I look at five, I get back to this NumString object. 5:01 So if I do a str(five) and there's '5' as a string. 5:07 And if I print(five), 5:14 I get the string version printed out which drops the quote marks. 5:16 What if we do int(five), then we get back a 5, and what if we do float(five)? 5:21 Then we get back 5.0. 5:28 So cool. 5:29 We have an object that represents itself as a string, but 5:30 that we can turn into a float or init. 5:33 Now, how is this different from any other number in a string? 5:35 It's not. 5:39 But hopefully you've gotten a peek behind the curtain here. 5:39 Now what happens if we try and do some math with our Numstring. 5:43 Any guesses before I type this out? 5:47 So I'm going to clear the screen real quick. 5:50 So let's try five + 4. 5:53 Type error. 5:55 We can't add Numstrings at int. 5:56 Fair enough, so what if we do five + a NumString that represents four? 5:59 Can't add NumStrings, another type error. 6:08 What about five + the string of '4'? 6:09 Type error. 6:16 Seems like no matter what I do, I can't do any math with it. 6:18 Let's see if we can fix that in the next video. 6:22
You need to sign up for Treehouse in order to download course files.Sign up