1 00:00:00,310 --> 00:00:04,230 So far, all of our methods have operated on an instance of our classes. 2 00:00:04,230 --> 00:00:06,440 They're what we might call instance methods. 3 00:00:06,440 --> 00:00:09,800 There's a special type of method in Python though that lets us construct an instance 4 00:00:09,800 --> 00:00:13,300 of our class without having to have a class instance already. 5 00:00:13,300 --> 00:00:15,230 We've seen one of them, new. 6 00:00:15,230 --> 00:00:17,330 But its specialness was built into Python. 7 00:00:17,330 --> 00:00:20,766 We can get this behavior on any method we want by making what's known as 8 00:00:20,766 --> 00:00:21,590 a class method. 9 00:00:21,590 --> 00:00:22,800 Let's check them out in WorkSpaces. 10 00:00:24,520 --> 00:00:27,740 Class methods give us a slightly more interesting way to do object 11 00:00:27,740 --> 00:00:29,060 instantiation. 12 00:00:29,060 --> 00:00:32,647 Now while they don't replace, they init in new methods. 13 00:00:32,647 --> 00:00:36,050 They often act as what's called a factory for an object. 14 00:00:36,050 --> 00:00:37,963 For instance let's make a book class. 15 00:00:37,963 --> 00:00:39,994 Okay, so I have a file here called books.py. 16 00:00:39,994 --> 00:00:42,530 You'll wanna go ahead and make that if you don't have it. 17 00:00:42,530 --> 00:00:47,781 And I'm gonna make a book, and I'm gonna make a bookcase, all right. 18 00:00:47,781 --> 00:00:48,980 So bookcase. 19 00:00:50,110 --> 00:00:51,530 And let's fill out book real quick. 20 00:00:52,830 --> 00:00:57,880 So we'll say for the init of a book we have a title and we have an author. 21 00:00:57,880 --> 00:01:03,800 And we'll say self.title = title and self.author = author. 22 00:01:03,800 --> 00:01:08,690 And let's make a __str__ for this. 23 00:01:08,690 --> 00:01:12,557 And we'll return that it's blah by blah. 24 00:01:12,557 --> 00:01:18,160 And we'll format that with self.title and self.author cool, all right. 25 00:01:18,160 --> 00:01:20,973 So now let's create our Bookcase. 26 00:01:22,459 --> 00:01:28,080 So it takes self, and it takes a list of books, which we're gonna say is none. 27 00:01:28,080 --> 00:01:32,010 Because you don't want to provide a mutable object 28 00:01:32,010 --> 00:01:35,390 as the default argument to a argument. 29 00:01:37,110 --> 00:01:39,990 So I say self dot books is equal to books. 30 00:01:39,990 --> 00:01:42,850 And now let's create our class method. 31 00:01:42,850 --> 00:01:49,420 So first of all we put this, and it's @classmethod. 32 00:01:49,420 --> 00:01:51,840 And I'll explain what that means in just a minute. 33 00:01:51,840 --> 00:01:56,379 And then we're gonna do a method here that we're gonna call create_bookcase(). 34 00:01:56,379 --> 00:01:58,294 And it takes an argument called class, cls, and 35 00:01:58,294 --> 00:01:59,850 it takes an argument called booklist. 36 00:01:59,850 --> 00:02:06,550 books equals an empty list and for title and 37 00:02:06,550 --> 00:02:13,660 author in Book list, books.append, book with the title and the author. 38 00:02:15,720 --> 00:02:16,740 And then in the end, 39 00:02:16,740 --> 00:02:22,530 we'll return an instance of the class with our list of books. 40 00:02:22,530 --> 00:02:27,860 Now class methods don't take self as their first argument, 41 00:02:27,860 --> 00:02:29,840 or the instance, or anything like that. 42 00:02:29,840 --> 00:02:33,000 Instead they take the class that they're being called on and 43 00:02:33,000 --> 00:02:35,810 we can't use the word class here. 44 00:02:35,810 --> 00:02:41,210 We can't do class because class is a reserve key word, right? 45 00:02:42,620 --> 00:02:44,220 Class has to be reserved for this. 46 00:02:44,220 --> 00:02:45,744 We can't use class here. 47 00:02:45,744 --> 00:02:48,990 So you'll see cls used a lot. 48 00:02:48,990 --> 00:02:50,730 It's a very common abbreviation for that. 49 00:02:50,730 --> 00:02:52,720 You also sometimes see klass, 50 00:02:53,780 --> 00:02:56,890 that's usually used in more meta programming kind of things. 51 00:02:56,890 --> 00:02:59,740 So cls is a good default. 52 00:02:59,740 --> 00:03:01,730 Inside the method we're not really doing anything special. 53 00:03:02,760 --> 00:03:06,310 But notice at the end that we used the class that was passed in 54 00:03:06,310 --> 00:03:07,630 to create a new instance. 55 00:03:07,630 --> 00:03:10,740 And we passed a list of books that we've now created. 56 00:03:10,740 --> 00:03:15,740 And that'll use the class's __init__ up here to do its constructory thing. 57 00:03:15,740 --> 00:03:18,450 So nice, we've encapsulated or 58 00:03:18,450 --> 00:03:23,270 contained some logic to make creating a case full of books easier. 59 00:03:23,270 --> 00:03:28,390 Now something you might not of seen before is this @ symbol here before classmethod. 60 00:03:28,390 --> 00:03:32,680 This symbol marks classmethod as a decorator. 61 00:03:32,680 --> 00:03:36,660 A decorator is more or less a function that takes another function, 62 00:03:36,660 --> 00:03:40,330 does something with it, and then usually returns that function. 63 00:03:40,330 --> 00:03:42,661 The class method decorator, for example, 64 00:03:42,661 --> 00:03:46,319 does some modifying of the expectations of Python's object class. 65 00:03:46,319 --> 00:03:48,851 Which all of our classes are inherited from, so 66 00:03:48,851 --> 00:03:50,760 class methods can work how they do. 67 00:03:52,100 --> 00:03:52,880 Let's try this out. 68 00:03:54,950 --> 00:04:01,250 So python and we will say from books import Bookcase. 69 00:04:02,590 --> 00:04:06,365 And let's make a bookcase and we won't instantiate the Bookcase. 70 00:04:06,365 --> 00:04:11,551 We'll just say Bookcase.create_bookcase() and 71 00:04:11,551 --> 00:04:15,090 then we pass it our list of books. 72 00:04:15,090 --> 00:04:22,492 So let's say Moby-Dick by Herman Melville, 73 00:04:22,492 --> 00:04:29,394 Jungle Book by Rudyard Kipling. 74 00:04:29,394 --> 00:04:32,230 I have two whole books on my bookcase. 75 00:04:33,660 --> 00:04:38,970 And now, if I look at Bookcase, I have this bookcase object. 76 00:04:38,970 --> 00:04:42,180 And I know that this has an attribute in there called books. 77 00:04:42,180 --> 00:04:45,930 So let's say bc.books and there's two books. 78 00:04:45,930 --> 00:04:47,780 And let's look at book zero. 79 00:04:49,130 --> 00:04:51,964 Let's do a string version of that. 80 00:04:54,739 --> 00:04:57,470 And we get Moby Dick by Herman Melville, so cool. 81 00:04:57,470 --> 00:05:00,710 It created the books for us and put them onto our bookcase. 82 00:05:00,710 --> 00:05:02,410 There's another special type of method, 83 00:05:02,410 --> 00:05:06,900 known as a static method, that doesn't require an instance or a class at all. 84 00:05:06,900 --> 00:05:09,280 These methods are mostly for design purposes, 85 00:05:09,280 --> 00:05:13,485 they let us put code into a class purely because it belongs with the class. 86 00:05:13,485 --> 00:05:14,365 Most of the time though, 87 00:05:14,365 --> 00:05:19,115 static methods can be done more cleanly as plain old functions and used scripts. 88 00:05:19,115 --> 00:05:21,745 Static methods can provide a nice set up design and organization and 89 00:05:21,745 --> 00:05:23,787 they can be overridden through class inheritance. 90 00:05:23,787 --> 00:05:25,635 But most of the time they're not all that useful.