Python Forum
@staticmethod and @classmethod
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
@staticmethod and @classmethod
#1
hi
the below code is in address:
https://python101.pythonlibrary.org/chap...ators.html
# decorator in python: @staticmethod & @classmethod usage
# from address:https://python101.pythonlibrary.org/chapter25_decorators.html


class DecoratorTest(object):
    """
    Test regular method vs @classmethod vs @staticmethod
    """

    def __init__(self):
        """Constructor"""
        pass

    def doubler(self, x):
        """"""
        print("running doubler")
        return x*2

    @classmethod
    def class_tripler(klass, x):
        """"""
        print("running tripler: %s" % klass)
        return x*3

    @staticmethod
    def static_quad(x):
        """"""
        print("running quad")
        return x*4

if __name__ == "__main__":
    decor = DecoratorTest()
    print(f"decor.doubler(5) results: {decor.doubler(5)}" )
    print("-"*50)
    print(f"decor.class_tripler(3) results: {decor.class_tripler(3)}")
    print("-"*50)
    print(f"DecoratorTest.class_tripler(3) results:{DecoratorTest.class_tripler(3)}" )
    print("-"*50)
    print(f"DecoratorTest.static_quad(2) results: {DecoratorTest.static_quad(2)}")
    print("-"*50)
    print(f"decor.static_quad(3) results: {decor.static_quad(3)}")
    print("-"*50)
    print("-"*50)
    print("-"*50)
    print(f"decor.doubler results: {decor.doubler}")
    print("-"*50)
    print(f"decor.class_tripler results: {decor.class_tripler}")
    print("-"*50)
    print(f"decor.static_quad results: {decor.static_quad}")
    print("-"*50)
my questions :
1)what is the klass argument in def class_tripler(klass, x)? if I write decor.class_tripler('test',3), causes a TypeError error message, but the method has two arguments.
2) On the same page is written that
Quote:You will also note that the last print statement shows that decor.static_quad returns a regular function instead of a bound method.
. what are the meanings of regular function and bound function?
thanks
Reply


Messages In This Thread
@staticmethod and @classmethod - by akbarza - Jan-01-2024, 07:30 AM
RE: @staticmethod and @classmethod - by Gribouillis - Jan-01-2024, 07:56 AM
RE: @staticmethod and @classmethod - by deanhystad - Jan-01-2024, 03:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorator staticmethod Use Cases Devarishi 3 2,788 May-20-2019, 04:27 AM
Last Post: Devarishi
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 1,900 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython
  How can i call classmethod with myClass() instead of using myClass.myClassMethod() harun2525 9 5,739 Mar-23-2017, 10:14 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020