1 00:00:00,000 --> 00:00:02,580 Let's take a look at a "dunder" method. 2 00:00:02,580 --> 00:00:07,000 Now, we've seen this already with a class, any class called 3 00:00:07,000 --> 00:00:11,100 'Test', and we said 'def __init__' 4 00:00:11,110 --> 00:00:14,110 This '__' is called a 'double underscore', 5 00:00:14,110 --> 00:00:17,400 or in Python terms, we call it a 'dunder'. 6 00:00:17,400 --> 00:00:23,500 And this '__init__' method here is called a 'dunder' method. 7 00:00:23,520 --> 00:00:26,280 And Python actually gives us a bunch of these to work with 8 00:00:26,280 --> 00:00:27,300 right out of the box. 9 00:00:27,300 --> 00:00:29,600 And in this lesson, we're going to cover three of those. 10 00:00:31,300 --> 00:00:34,400 So let's go ahead and create an example here where we have 11 00:00:34,400 --> 00:00:43,100 a book, 'class Book:', and 'def __init__(self,', and the book 'title', and 12 00:00:43,110 --> 00:00:44,860 total 'pages', I guess, 13 00:00:44,860 --> 00:00:46,510 and maybe the 'content' of the book 14 00:00:46,520 --> 00:00:48,610 I guess. This is just an example. 15 00:00:48,610 --> 00:00:51,800 So we do 'self.title = title', 16 00:00:51,820 --> 00:00:53,890 'self.pages =, 17 00:00:53,890 --> 00:00:58,840 let's cast this as an integer, 'int(pages)', and 'self.content' 18 00:00:58,840 --> 00:01:00,800 is going to be whatever the 'content' is. 19 00:01:01,400 --> 00:01:02,700 So let's create a new book in here. 20 00:01:03,100 --> 00:01:08,700 'harry_potter', a book you may or may not have heard of, this is 21 00:01:08,700 --> 00:01:12,800 going to be a new 'Book()', 'title = "Harry Potter and the Prisoner 22 00:01:12,800 --> 00:01:14,100 of Azkaban', or something. I don't know, 23 00:01:14,100 --> 00:01:17,300 is it 250 pages, something like that, and then you know some 'content' in here. 24 00:01:17,300 --> 00:01:20,100 And that could actually be the book contents if we wanted to. Okay. 25 00:01:20,100 --> 00:01:22,200 So now we have this new class instantiated. 26 00:01:22,200 --> 00:01:25,700 And if we just type 'harry_potter', we see that it's a 'Book' 27 00:01:25,700 --> 00:01:27,600 at some memory location. 28 00:01:28,300 --> 00:01:33,200 And if we do 'type(harry_potter)', it just says it's a 'Book'. 29 00:01:33,200 --> 00:01:34,300 So this one is okay. 30 00:01:34,300 --> 00:01:38,100 But what if you wanted a better name for this? 31 00:01:38,100 --> 00:01:40,200 What if you didn't just want it to be named whatever the 32 00:01:40,220 --> 00:01:43,360 memory location is? There is a nicer way to deal with it. 33 00:01:43,360 --> 00:01:47,300 And you actually do use 'class names' a lot. 34 00:01:47,300 --> 00:01:51,700 And so one way we do that, or really the way we do that 35 00:01:51,700 --> 00:01:53,800 is with a 'dunder' method called, string, '__str__', 36 00:01:53,800 --> 00:01:56,200 and so that's going to take 'self', 37 00:01:56,200 --> 00:01:59,300 and whatever we return in here is what this class is 38 00:01:59,300 --> 00:02:00,500 going to be called. So, 39 00:02:00,520 --> 00:02:01,780 "Test Book". 40 00:02:01,780 --> 00:02:04,390 Okay. So I'm just going to rerun these cells, and we're going to 41 00:02:04,390 --> 00:02:07,700 see that 'harry_potter' is still pointing to some memory 42 00:02:07,730 --> 00:02:08,770 location, and that's okay. 43 00:02:08,800 --> 00:02:11,500 But what we want to do is print this out. 44 00:02:11,500 --> 00:02:13,800 If we print this out, it says "Test Book". 45 00:02:13,850 --> 00:02:16,030 So let's go ahead and actually delete this, because I got 46 00:02:16,030 --> 00:02:17,100 one step ahead of us here. 47 00:02:17,120 --> 00:02:19,400 Rerun this. Again, 48 00:02:19,410 --> 00:02:22,700 when we print out 'harry_potter' without the 'dunder', '__str__' 49 00:02:22,710 --> 00:02:26,230 in there, it just points to a location in our memory. 50 00:02:26,900 --> 00:02:31,600 But if we put '__str__', because we have 51 00:02:31,680 --> 00:02:34,640 this in here, that is the string representation of this 'Book'. 52 00:02:34,640 --> 00:02:38,700 Now, what we can actually do is change this to be 'self.title'. 53 00:02:38,700 --> 00:02:40,500 Rerun that, that, and that, 54 00:02:40,580 --> 00:02:42,200 and this is called "Harry Potter". 55 00:02:42,210 --> 00:02:44,510 Now we can start another book. 56 00:02:44,520 --> 00:02:45,860 I'm going to get rid of 'type' here. 57 00:02:46,500 --> 00:02:49,800 And let's call it Lord of the Rings, 'lotr = Book( 58 00:02:50,900 --> 00:02:53,400 "Lord of the Rings". I don't know, 59 00:02:53,410 --> 00:02:55,740 it's a 900 page book? Sometimes feels like it. 60 00:02:55,750 --> 00:02:57,150 And it's got some 'content' in there. 61 00:02:57,900 --> 00:03:03,400 We can do 'print()', Lord of the Rings, 'lotr', and we now know what this class 62 00:03:03,480 --> 00:03:04,730 is, it's called "Lord of the Rings". 63 00:03:04,800 --> 00:03:10,500 We know it's a 'Book', because if we do 'type(lotr)', 64 00:03:10,500 --> 00:03:12,500 [no audio] 65 00:03:12,500 --> 00:03:13,500 it is a 'Book'. 66 00:03:13,500 --> 00:03:16,000 Now let's go ahead and add a little bit more to this. 67 00:03:16,000 --> 00:03:19,110 What if we wanted to get the total length of the book, 68 00:03:19,120 --> 00:03:20,340 like how many pages there? 69 00:03:20,340 --> 00:03:22,400 We could actually just use 'Book.pages', 70 00:03:22,480 --> 00:03:26,410 but what if we wanted to get the total length, the total number 71 00:03:26,620 --> 00:03:34,130 of words, for instance? So we could do 'def __len__(self):', 72 00:03:34,500 --> 00:03:39,700 and just as an example, let's return 150, just for funsies. 73 00:03:39,700 --> 00:03:41,900 We'll rerun that, that, 74 00:03:41,900 --> 00:03:48,200 and in here we can run 'len(harry_potter)', and it gives us 150. 75 00:03:48,200 --> 00:03:50,500 So now we can actually perform some logic in here. 76 00:03:50,580 --> 00:03:57,520 We can say 'words = self.content.split(' ')', and then 77 00:03:57,520 --> 00:04:00,900 return the total 'len(words)'. 78 00:04:01,700 --> 00:04:04,000 Now we actually need to have some words in here, 79 00:04:04,000 --> 00:04:06,500 and so I just filled this with 'Lorem ipsum', 80 00:04:06,500 --> 00:04:08,600 it's 25 words of 'Lorem ipsum'. 81 00:04:08,640 --> 00:04:09,720 No idea what it means. 82 00:04:09,730 --> 00:04:10,800 I hope it's not offensive. 83 00:04:10,810 --> 00:04:16,050 And when I rerun 'len(harry_potter), it's going to get the total 84 00:04:16,050 --> 00:04:17,700 content, which is this one here. 85 00:04:17,760 --> 00:04:21,660 It's going to split it by every single space, and then it's 86 00:04:21,670 --> 00:04:23,279 going to return the total number of words. 87 00:04:24,600 --> 00:04:25,600 And there we go. 88 00:04:25,600 --> 00:04:28,200 We have 25 words of 'Lorem Ipsum' in there. That's exactly what I 89 00:04:28,200 --> 00:04:30,300 put into it, 25 words of 'Lorem ipsum'. 90 00:04:30,300 --> 00:04:34,700 So these are just three 'dunder' methods that come with 91 00:04:34,700 --> 00:04:37,200 pretty much every class, and you can overwrite them to be whatever 92 00:04:37,200 --> 00:04:39,900 you want, and let's see what other methods there are. 93 00:04:40,630 --> 00:04:44,040 So let's add a new cell here. 94 00:04:44,200 --> 00:04:49,500 And in here, let's do 'harry_potter.', hit 'Tab', 95 00:04:49,500 --> 00:04:51,500 but then if you write an '_', you can actually see that 96 00:04:51,590 --> 00:04:53,340 there's 'class', 97 00:04:53,340 --> 00:04:59,800 'del', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', all sorts 98 00:04:59,800 --> 00:05:03,200 of stuff in here, and these are all magic methods. 99 00:05:03,200 --> 00:05:06,200 These are 'dunder' methods that come with Python. 100 00:05:06,200 --> 00:05:09,800 A few of these you're going to see a lot more than other ones though. 101 00:05:09,820 --> 00:05:11,740 And some of these you probably won't ever see. 102 00:05:11,840 --> 00:05:14,720 So 'class' you will probably see in a framework like Django, 103 00:05:16,000 --> 00:05:17,300 'del', or 'delattr', 104 00:05:17,350 --> 00:05:21,060 you'll probably see that in data science stuff where you need 105 00:05:21,070 --> 00:05:22,500 to free up memory pretty fast. 106 00:05:22,640 --> 00:05:24,530 'dict', is actually a pretty cool one. 107 00:05:24,540 --> 00:05:27,320 I use this one for debugging a lot, and this one just turns 108 00:05:27,320 --> 00:05:29,100 your whole class into a dictionary. 109 00:05:29,100 --> 00:05:31,400 So you've got 'title', 'pages', and 'content' in there. 110 00:05:31,400 --> 00:05:32,800 That's pretty cool. That's a good one. 111 00:05:32,860 --> 00:05:37,690 We also have 'dir', which will show you all sorts of stuff, 112 00:05:37,700 --> 00:05:42,700 all the attributes, as well as all of the methods that are available in here. 113 00:05:42,790 --> 00:05:46,020 So that's a good one to sort of discover what a class has 114 00:05:46,100 --> 00:05:49,700 if you have not written the class yourself. 'init', yep, 115 00:05:49,710 --> 00:05:52,310 we've used that one a lot. 'init_subclass' is a good one. 116 00:05:52,420 --> 00:05:56,230 'len' we've looked at already. 'repr', 117 00:05:56,290 --> 00:05:58,000 you might end up seeing that one as well. 118 00:05:58,000 --> 00:05:59,000 That's a good one to set, but 119 00:05:59,000 --> 00:06:02,110 that's a, that's a best practice that a lot of people, as far as 120 00:06:02,110 --> 00:06:04,700 I can tell, don't tend to follow but it's a good one. 121 00:06:04,700 --> 00:06:06,600 And 'str', 122 00:06:06,600 --> 00:06:08,000 so that's the string representation. 123 00:06:08,070 --> 00:06:09,800 That's the name of the current class. 124 00:06:09,900 --> 00:06:12,500 And, yeah, there you go. So 'dunder' methods, 125 00:06:12,500 --> 00:06:14,600 there are a lot of them, a lot of them you probably won't 126 00:06:14,600 --> 00:06:15,800 use on a day to day basis. 127 00:06:15,800 --> 00:06:17,000 Some of them you will. 128 00:06:17,080 --> 00:06:20,170 I use 'init' and 'str' almost every single day. 129 00:06:20,180 --> 00:06:23,200 I've used 'repr' quite a bit, and 'len' is a good one, 130 00:06:23,260 --> 00:06:26,140 but as a web developer, I use Django, 131 00:06:26,150 --> 00:06:28,270 and I don't really need to use that one too much, because it 132 00:06:28,270 --> 00:06:30,200 usually comes with classes. 133 00:06:30,280 --> 00:06:34,060 But should you ever need to do that, you can do that. 134 00:06:34,070 --> 00:06:36,550 So if you ever create a digital bookstore and you want to 135 00:06:36,700 --> 00:06:40,900 use the number of words as the total book length, you can do that, too. 136 00:06:40,920 --> 00:06:44,630 You can get the total number of words and the average number 137 00:06:44,640 --> 00:06:47,000 of words that a human reads per minute, 138 00:06:47,010 --> 00:06:50,800 and then you could say that a "Book's length is, in minutes, 139 00:06:50,800 --> 00:06:53,600 180 minutes", something like that. 140 00:06:53,600 --> 00:06:57,850 Anyways, there are uses for it, but these are three pretty 141 00:06:57,860 --> 00:07:00,340 popular ones that I thought I would like to just sort of 142 00:07:00,350 --> 00:07:03,400 introduce you to and get you a little more familiar with.