Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

Avatar image for Zarata

Zarata on April 20, 2020

Sorry … digestible.

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.

Avatar image for Erikton Konomi

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/

Avatar image for sroux53

sroux53 on June 8, 2020

Excellent !

Avatar image for Ghani

Ghani on Oct. 7, 2020

Good but “too compact”!

Avatar image for Mark Walle

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/

Avatar image for boardtc

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?

Avatar image for ephremworkeye

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

Avatar image for Martin Breuss

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?

Avatar image for Tony Florido

Tony Florido on Dec. 26, 2021

@ephremworkeye instance of the class.

Become a Member to join the conversation.