Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Constructicons5:26 with Kenneth Love
What if we need something more complex than just a custom init or new method?
Constructors, as most
classmethods would be considered, are a common sight in other languages. They're less common in Python but still really useful. I highly recommend playing around with them outside of this course and getting comfortable with them.
staticmethods, they're...weird. A
staticmethod is a method that doesn't require an instance (
self) or a class (
cls). So they belong to a class because, logically, they belong there. Most of the time, though, you're better served by just creating a function in the same module as your class.
So, would it be possible to do
create_bookcase without it being a
classmethod? Yes and no. We could write the exact same method with a few differences, like using
self instead of
cls, and then leave off the decorator. We'd have to create an instance of the class first, though. It'd look something like this:
def create_bookcase(self, book_list): for author, title in book_list: self.append(Book(author, title)) return self
We could leave off that last line, too, but it's generally a best practice to always have functions and methods return. Our use of it becomes a little weird to write out, though.
>>> Bookcase().create_bookcase([("Eric Matthes", "Crash Course Python")])
We have to create the instance first, with
Bookcase() and then call the method. By using a class method, we move the instance creation into the method where it makes more sense. It's a small and fairly subtle design decision but it makes for a nicer interaction in the end.
So far, all of our methods have operated on an instance of our classes. 0:00 They're what we might call instance methods. 0:04 There's a special type of method in Python though that lets us instruct an instance 0:06 of our class without having to have a class instance already. 0:09 We've seen one of them, new. 0:13 But it's specialness was built into Python. 0:15 We can get this behavior on any method we want by making what's known as 0:17 a class method. 0:20 Let's check them out in WorkSpaces. 0:21 Class methods give us a slightly more interesting way to do object 0:24 instantiation. 0:27 Now while they don't replace, they init in new methods. 0:29 They often act as what's called a factory for an object. 0:32 For instance let's make a book class. 0:36 Okay, so I have a file here called books.py. 0:37 You'll wanna go ahead and make that if you don't have it. 0:39 And I'm gonna make a book, and I'm gonna make a bookcase, all right. 0:42 So bookcase. 0:47 And let's fill out book real quick. 0:50 So we'll say for the init of a book we have a title and we have an author. 0:52 And we'll say self.title = title and self.author = author. 0:57 And let's make a __str__ for this. 1:03 And we'll return that it's blah by blah. 1:08 And we'll format that with self.title and self.author cool, all right. 1:12 So now let's create our Bookcase. 1:18 So it takes self, and it takes a list of books, which we're gonna say is none. 1:22 Because you don't want to provide a mutable object 1:28 as the default argument to a argument. 1:32 So I say self-taught books is equal to books. 1:37 And now let's create our class method. 1:39 So first of all we put this, and it's @classmethod. 1:42 And I'll explain what that means in just a minute. 1:49 And then we're gonna do a method here that we're gonna call a create bookcase. 1:51 And it takes an argument called class, cls, and 1:56 it takes an argument called booklist. 1:58 Books equals an empty list and for title and 1:59 author in book list, books.append, book with the title and the author. 2:06 And then in the end, 2:15 we'll return an instance of the class with our list of books. 2:16 Now class methods don't take self as their first argument, 2:22 or the instance, or anything like that. 2:27 Instead they take the class that they're being called on And 2:29 we can't use the word class here. 2:33 We can't do class because class is a reserve key word, right? 2:35 Class has to be reserved for this. 2:42 We can't use class here. 2:44 So you'll see CLS used a lot. 2:45 It's a very common abbreviation for that. 2:48 You also sometimes see KLASS, 2:50 that's usually used in more meta programming kind of things. 2:53 So CLS is a good default. 2:56 Inside the method we're not really doing anything special. 2:59 But notice at the end that we used the class that was passed in 3:02 to create a new instance. 3:06 And we passed a list of books that we've now created. 3:07 And that'll use the classes a nit appear to do its constructory thing. 3:10 So nice, we've encapsulated or 3:15 contained some logic to make creating a case full of books easier. 3:18 Now something you might not of seen before is this @ symbol here before classmethod. 3:23 This symbol marks classmethod as a decorator. 3:28 A decorator is more or less a function that takes another function, 3:32 does something with it, and then usually returns that function. 3:36 The class method decorator, for example, 3:40 does some modifying of the expectations of Python's object class. 3:42 Which all of our classes are inherited from, so 3:46 class methods can work how they do. 3:48 Let's try this out. 3:52 So python and we will save from books import bookcase. 3:54 And let's make a bookcase and we won't instantiate the bookcase. 4:02 We'll just say bookcase.create_bookcase and 4:06 then we pass it our list of books. 4:11 So let's say Moby-Dick by Herman Melville, 4:15 Jungle Book by Rudyard Kipling. 4:22 I have two whole books on my bookcase. 4:29 And now, if I look at bookcase, I have this bookcase object. 4:33 And I know that this has an attribute in there called books. 4:38 So let's say bc.books and there's two books. 4:42 And let's look at book zero. 4:45 Let's do a string version of that. 4:49 And we get Moby Dick by Herman Melville, so cool. 4:54 It created the books for us and put them onto our bookcase. 4:57 There's another special type of method, 5:00 known as a static method, that doesn't require an instance or a class at all. 5:02 These methods are mostly for design purposes, 5:06 they let us put code into a class purely because it belongs with the class. 5:09 Most of the time though, 5:13 static methods can be done more cleanly as plain old functions and used scripts. 5:14 Static methods can provide a nice set up design and organization and 5:19 they can be overridden through class inheritance. 5:21 But most of the time they're not all that useful. 5:23
You need to sign up for Treehouse in order to download course files.Sign up