1 00:00:00,000 --> 00:00:04,989 [MUSIC] 2 00:00:04,989 --> 00:00:08,685 Now that we have a handle on creating classes, let's talk about one of the most 3 00:00:08,685 --> 00:00:11,498 powerful features of Python's classes, magic methods. 4 00:00:11,498 --> 00:00:14,647 You've already seen one magic method, the __init__ one, 5 00:00:14,647 --> 00:00:18,509 remember we call it magic because it's a method that Python will call for you. 6 00:00:18,509 --> 00:00:21,759 As you can probably imagine, there are tons of Magic Methods that Python uses 7 00:00:21,759 --> 00:00:24,330 behind the scenes to make things run smoothly. 8 00:00:24,330 --> 00:00:25,800 You'll be learning about some of them, but 9 00:00:25,800 --> 00:00:29,160 I've linked to the full documentation about them in the teacher's notes. 10 00:00:29,160 --> 00:00:32,140 I really like Magic Methods, because despite their name, 11 00:00:32,140 --> 00:00:35,990 they let us peel back some of the magic in Python and see how things are working. 12 00:00:35,990 --> 00:00:39,550 One of the things that happens behind the scenes, is conversion between types. 13 00:00:39,550 --> 00:00:42,300 Think about when you use the str function on an integer. 14 00:00:42,300 --> 00:00:43,190 What happens? 15 00:00:43,190 --> 00:00:45,130 It's actually controlled by a Magic Method. 16 00:00:45,130 --> 00:00:46,460 Let's go to WorkSpaces and check it out. 17 00:00:47,660 --> 00:00:50,570 Let's start with our illustrious Thief class. 18 00:00:50,570 --> 00:00:54,300 Right now if we turn a Thief into a string, 19 00:00:54,300 --> 00:00:58,760 we get something that's well, less than useful. 20 00:00:58,760 --> 00:01:00,100 Not really that great. 21 00:01:00,100 --> 00:01:03,070 So remember that the 0x blah, blah, 22 00:01:03,070 --> 00:01:06,010 blah bit refers to where the object is stored in memory. 23 00:01:07,310 --> 00:01:10,120 Handy but not really handy. 24 00:01:11,470 --> 00:01:14,530 Wouldn't it be easier to have it tell us the name of the character? 25 00:01:14,530 --> 00:01:16,820 Well, let's make it do that then. 26 00:01:16,820 --> 00:01:20,160 I think this is probably something we'd want all characters to have though, 27 00:01:20,160 --> 00:01:24,040 so we'll do it on the Character class and not on the Thief class. 28 00:01:25,300 --> 00:01:29,680 You'll often find the implement Magic Methods on your base classes, but 29 00:01:29,680 --> 00:01:32,680 not on your specific implementation classes. 30 00:01:32,680 --> 00:01:34,160 Those that you use directly, 31 00:01:34,160 --> 00:01:37,750 usually only have very specific code that they need to do their jobs. 32 00:01:37,750 --> 00:01:41,170 The more generic code goes into the parent classes further up the chain. 33 00:01:42,390 --> 00:01:44,760 So let's add a new method here. 34 00:01:47,746 --> 00:01:51,883 And we'll call it __str__. 35 00:01:51,883 --> 00:01:53,248 As you probably could have guessed. 36 00:01:55,028 --> 00:01:56,240 So we actually don't need that line. 37 00:01:57,980 --> 00:02:03,130 So this method should return a string that you want to use to 38 00:02:03,130 --> 00:02:07,140 identify your object, whenever it's turned into a string. 39 00:02:07,140 --> 00:02:09,117 So in this case, let's return. 40 00:02:12,087 --> 00:02:13,172 Let's return two things. 41 00:02:13,172 --> 00:02:19,274 Let's return the self.__class__.__name__, so we can see if it's a thief, 42 00:02:19,274 --> 00:02:24,417 or an archer, or a barbarian, or whatever and let's return self.name. 43 00:02:24,417 --> 00:02:30,524 So now, if we recheck what happens when we run play.py. 44 00:02:31,650 --> 00:02:35,849 We see Thief: Kenneth, so that's awesome, that's very cool, that's much, 45 00:02:35,849 --> 00:02:38,240 much friendlier, and it's better output. 46 00:02:38,240 --> 00:02:43,416 A related method that's often on a written is __repr__, __repr__ 47 00:02:43,416 --> 00:02:48,784 which is used to give the official string representation of an instance. 48 00:02:48,784 --> 00:02:50,725 Now this was mostly used for debugging and 49 00:02:50,725 --> 00:02:53,660 should present as much useful information as possible. 50 00:02:53,660 --> 00:02:57,310 Right now, our rpg gives back what our old string looked like, and 51 00:02:57,310 --> 00:02:59,680 I think that's good output for it, so I'm gonna leave that alone. 52 00:03:00,820 --> 00:03:04,700 So that's converting to a string, but what about other conversions? 53 00:03:04,700 --> 00:03:07,760 More complex conversions, like turning an instance into a dictionary or 54 00:03:07,760 --> 00:03:11,700 other object while possible, are beyond the scope of this course. 55 00:03:11,700 --> 00:03:16,313 We can though easily set up converting our objects into floats. 56 00:03:16,313 --> 00:03:19,583 Now I don't think any of our existing classes would work well for this, so 57 00:03:19,583 --> 00:03:20,599 let's make a new one. 58 00:03:20,599 --> 00:03:25,477 And we'll do this out here in the OO Python directory not in the rpg directory. 59 00:03:25,477 --> 00:03:29,330 Now I'm going to call this numstring.py. 60 00:03:29,330 --> 00:03:33,306 Let's make a class that holds a number, holds an int or a float, or 61 00:03:33,306 --> 00:03:34,660 whatever as a string. 62 00:03:36,060 --> 00:03:40,320 But, it'll let us turn it into an int whenever we want to. 63 00:03:40,320 --> 00:03:46,072 Let's just do this, so we'll say class NumString, cuz that's a catchy name. 64 00:03:47,675 --> 00:03:51,434 And then def __init__ (self and value). 65 00:03:51,434 --> 00:03:57,660 And self.value = str (value). 66 00:03:57,660 --> 00:04:02,959 And then we'll define the __str__, and we'll return self.value. 67 00:04:04,730 --> 00:04:11,458 And we'll define __int__, and we'll return the int(self.value). 68 00:04:13,058 --> 00:04:20,160 And let's also define __float__, which returns the float(self.value). 69 00:04:20,160 --> 00:04:25,078 Now, I'm sure you could guess what __int__ and __float__ did. 70 00:04:25,078 --> 00:04:26,103 Even before I told you. 71 00:04:26,103 --> 00:04:27,310 Since you've seen __str__. 72 00:04:27,310 --> 00:04:29,350 But it's still good to see them. 73 00:04:31,020 --> 00:04:36,320 Hopefully, you can see why you probably wouldn't want to use this as real code. 74 00:04:36,320 --> 00:04:37,850 It's good for experimentation though. 75 00:04:37,850 --> 00:04:39,720 You should always play around, and 76 00:04:39,720 --> 00:04:43,370 try just whatever silly ideas you can come up with. 77 00:04:43,370 --> 00:04:46,130 So, in the interest of being silly, let’s try it out. 78 00:04:47,530 --> 00:04:50,935 Let’s come down here, and then go into python. 79 00:04:50,935 --> 00:04:56,482 And I’m going to say from numstring import NumString. 80 00:04:56,482 --> 00:05:00,397 And I’m going to say five = NumString (5). 81 00:05:01,847 --> 00:05:05,840 And if I look at five, I get back to this NumString object. 82 00:05:07,480 --> 00:05:14,340 So if I do a str(five) and there's '5' as a string. 83 00:05:14,340 --> 00:05:16,450 And if I print(five), 84 00:05:16,450 --> 00:05:21,982 I get the string version printed out which drops the quote marks. 85 00:05:21,982 --> 00:05:28,049 What if we do int(five), then we get back a 5, and what if we do float(five)? 86 00:05:28,049 --> 00:05:29,400 Then we get back 5.0. 87 00:05:29,400 --> 00:05:30,450 So cool. 88 00:05:30,450 --> 00:05:33,240 We have an object that represents itself as a string, but 89 00:05:33,240 --> 00:05:35,660 that we can turn into a float or init. 90 00:05:35,660 --> 00:05:39,070 Now, how is this different from any other number in a string? 91 00:05:39,070 --> 00:05:39,990 It's not. 92 00:05:39,990 --> 00:05:43,170 But hopefully you've gotten a peek behind the curtain here. 93 00:05:43,170 --> 00:05:47,939 Now what happens if we try and do some math with our NumString. 94 00:05:47,939 --> 00:05:50,739 Any guesses before I type this out? 95 00:05:50,739 --> 00:05:53,070 So I'm going to clear the screen real quick. 96 00:05:53,070 --> 00:05:55,440 So let's try five + 4. 97 00:05:55,440 --> 00:05:56,780 Type error. 98 00:05:56,780 --> 00:05:59,580 We can't add NumStrings at int. 99 00:05:59,580 --> 00:06:06,350 Fair enough, so what if we do five + a NumString that represents four? 100 00:06:08,070 --> 00:06:09,840 Can't add NumStrings, another type error. 101 00:06:09,840 --> 00:06:15,000 What about five + the string of '4'? 102 00:06:16,480 --> 00:06:16,980 Type error. 103 00:06:18,320 --> 00:06:22,140 Seems like no matter what I do, I can't do any math with it. 104 00:06:22,140 --> 00:06:23,770 Let's see if we can fix that in the next video.