Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Controlling Conversion 6:24
- Dream Car 1 objective
- Math 6:22
- Multiplication 2 objectives
- Emulating Built-ins 8:01
- Iterable Album 1 objective
- Subclassing Built-ins 10:28
- Double 3 objectives
- Frustration 1 objective
- Constructicons 5:26
- Dream Vacation 1 objective
- Special Methods 7:30
- Proper Properties 2 objectives

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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.
As for staticmethod
s, 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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Cameron Ganley
4,049 Points1 Answer
-
Hanwen Zhang
20,084 Points1 Answer
-
Robert Peters
3,728 Points1 Answer
-
Robert Peters
3,728 Points2 Answers
-
Michael Russell
1,575 Points1 Answer
-
Aizah Sadiq
2,435 Points1 Answer
-
Nicholas Abate
1,643 Points1 Answer
-
Kohei Ashida
4,882 PointsIn what kind of situations can class method be more useful than instance method?
Posted by Kohei AshidaKohei Ashida
4,882 Points1 Answer
-
Kohei Ashida
4,882 Points2 Answers
-
Kohei Ashida
4,882 PointsIs my understanding correct on what is "decorator"?
Posted by Kohei AshidaKohei Ashida
4,882 Points1 Answer
-
tomtrnka
9,780 Points1 Answer
-
HAROLD MORI
3,629 Points1 Answer
-
Prateek Sharan Lall
1,778 Points1 Answer
-
PLUS
MIAN OSAMA ARSHAD
Courses Plus Student 279 Points1 Answer
-
Anthony Grodowski
4,902 Points2 Answers
-
Juneau Lim
13,362 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up