@staticmethod vs @classmethod: Summary and Recap
In this summary and recap video you’ll get to go over what you have covered up to this point. Here’s what you have learned:
- What static methods are and how they are different from class methods
- How to use class methods for introspection
- Class methods are useful for returning instances of subclasses or caching
- Examples of class and static methods
- What to look out for when using class and static methods
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 In summary, static methods can be viewed as normal functions in Python which are grouped with a particular class. This grouping is merely a taste, something that’s an individual preference for developer to developer.
00:15 For the most part, they can be generally ignored. Anything you can do with a static method, you can do as a module-level method. They take x arguments and/or keyword arguments and don’t take into any account of any of the state that is around them.
00:30 The only reason that you would end up using the static method in a particular class is to group some logic together in a functional piece. Class methods, on the other hand, have a little bit of introspection that they can do.
00:44
They are also very good at creating new instances of objects. And as we showed here with the Dog
and Cat
example, they take into account subclasses when they’re being passed through, which make them very useful when doing things like returning new instances of subclasses or doing some type of caching. Again, you can do these things—whether you need to do them or if that is the best way to do them, that’s up for debate.
01:12 We covered a few examples to show you the uses of each, and things to look out for when using them. So, this will conclude our screen talk on static methods and class methods.
Become a Member to join the conversation.