Python Method Types Recap & Review
This is the last video of the course providing you a quick recap of what you have learned so far about the three different method types.
Want more? Keep improving your Python OOP skills with our Object-Oriented Programming (OOP) With Python Learning Path.
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 Just a quick recap. So we talked about plain methods, class methods, and static methods, and when you can call them. Pretty much all you need to remember is that a regular method needs an object instance to be called on; and in a class method, it needs a class and it has access to the class; and a static method doesn’t really have any access to the object, or an object instance, or the class at all, and it’s just kind of a way to namespace your functions. All right, so have fun exploring the pizza analogy here and building the perfect pizza API in an object-oriented fashion. Talk to you soon, and happy Pythoning!
Dan Bader RP Team on June 12, 2019
@sumankalyanr: Thanks, that’s great to hear!
victorariasvanegas on June 22, 2019
Great explanation , I’m really enjoying this class.
DiscreteLoner on June 27, 2019
Awesome! I really enjoy this video set.
Aditia A. Pratama on Aug. 9, 2019
super cool, it would be awesome if we get references to real world implementation as well, probably link to github/gitlab projects which has the usage of OOP Method
LJIN Lab on Aug. 10, 2019
Great tutorial, but real world examples would be great!
ThangHoang on Aug. 21, 2019
Awesome, Thank you so much
blaqhrse on Sept. 22, 2019
Thanks Dan; clear explanation but would like to echo the same as previous posts. Real world examples would really help drive it home.
Dan Bader RP Team on Sept. 23, 2019
Some inspiration for real-world use cases of @staticmethod
and @classmethod
:
mikesult on March 3, 2020
Thanks for another good tutorial and thanks for the links to stackoverflow. There’s a lot of different opinions to digest.
Zarata on April 20, 2020
I like the short, digestable (no Pizza pun intended, but I’ll take it), and on point. Thanks! IF I’m right and the “magic” is in the decorators to make things work one way or other, you might want to make special note of that. I’ve only seen decorator use here and there in the trails I’ve been following and I’ve already forgotten the bits I’ve seen.
Zarata on April 20, 2020
Sorry … digestible.
J on May 19, 2020
Im sorry man, but I didnt find good information from this tutorial. Perhaps more and different use cases. I would have preferred a longer tutorial if it meant more and clear guidance on this topic.
Erikton Konomi on May 20, 2020
Thanks Dan, but have to agree with others. The links from stackoverflow have some good information, but a couple solid examples in the tutorial itself would have been preferable.
I found this example helpful: stackabuse.com/pythons-classmethod-and-staticmethod-explained/
sroux53 on June 8, 2020
Excellent !
Ghani on Oct. 7, 2020
Good but “too compact”!
Mark Walle on Dec. 2, 2020
Here is a bit of a deeper dive on this subject matter that still involves pizza (found via one of Dan’s stackoverflow posts linked above):
julien.danjou.info/guide-python-static-class-abstract-methods/
boardtc on Jan. 27, 2021
@Mark Walle, thanks for link, that article explains about bounded & unbounded methods which gave me further understanding into the python way. One thing I wonder is does a method have to be either a class/cls method, a bound/self method, a static method or an abstract method…there are no other options for it to be just a method?
ephremworkeye on Aug. 16, 2021
Thank you so much!!! I have one question though: when we call @classmethod like Pizza.margherita(), does it mean we have created instance of class or object?
Thanks
Martin Breuss RP Team on Aug. 16, 2021
Hi @ephremworkeye. Is it correct that you’re thinking of a @classmethod
called margherita()
that’s part of the Pizza
class:
class Pizza:
@classmethod
def margherita(cls):
return "It's a pi'zza!"
This toy implementation doesn’t make much sense as a @classmethod
, considering that the cls
argument isn’t used in the body of the method. But since that’s not the point of your question, we can let it slide :)
So if you’re calling a @classmethod
called margherita()
that’s part of the Pizza
class, then you can get the following output:
>>> Pizza.margherita()
"It's a pi'zza!"
>>> Pizza.margherita() == Pizza().margherita()
True
>>> Pizza.margherita
<bound method Pizza.margherita of <class '__main__.Pizza'>>
Where do you think the @classmethod
belongs to when you look at this output, and what’s your interpretation of the results? Can you come up with another test that might help answer your question?
Tony Florido on Dec. 26, 2021
@ephremworkeye instance of the class.
Become a Member to join the conversation.
sumankalyanr on June 12, 2019
Super cool explanation on method types in python.. I thoroughly enjoyed it.