• Animal
  • Auto
  • Computers
  • Education
  • Finance
  • Health
  • Math
  • Mobiles
  • Programming
  • Science
  • Sports
  • Technology
  • Terms
  • Web


  • Difference between @StaticMethod and @ClassMethod in Python

    @StaticMethod vs @ClassMethod in Python

    @StaticMethod and @ClassMethod are type of methods or functions in Python. Both serves similar purpose but have differences between them.

    @StaticMethod and @ClassMethod are functions decorators. The first difference which is noticable is the call signatures of both. Class Method has @classmethod signature and Static method has @staticmethod.

    Check out the Difference Between Python and Perl

    Difference Between @ClassMethod and @StaticMethod in Python

    Below are the differences and please read the below code to understand the differences clearly.

    @StaticMethod

    • @StaticMethod is the signature which is used before the function declaration.
    • For Static methods, neither self (the object instance) nor cls (the class) is implicitly passed as the first argument.
    • @StaticMethod should be called using with instance of the class. For ex: objA is the instance of Class A and static_print is @staticmethod then, It should be a.static_print()
    • A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the arguments that were passed, no implicit first argument. It is can be part of a class but should be used when you don’t want to access the member variables of the class. Alternatively, You could a module function instead of a staticmethod.

    @ClassMethod in Python

    • @ClassMethod is the signature which is used before the function declaration.
    • For Class methods, the class of the object instance is implicitly passed as the first argument instead of self.
    • @ClassMethod should be called using with Class Name. For ex: A is the class and class_print is class method then. It should be A.class_print()
    • @ClassMethod in Python codename was Blue
    • A classmethod, on the other hand, is a method that gets passed the class it was called on, or the class of the instance it was called on, as first argument. This is useful when you want the method to be a factory for the class.

    The class methods are used to create inheritable alternative constructors.

    These are the Differences between @ClassMethod and @StaticMethod in Python..

    Click here for more info on @classmethod and @staticmethod

    Posted under: Programming
    1957 readers are already subscribed to this blog! Why don't you be one of them? Subscribe to this blog via your favorite RSS feed reader or by entering your email address on the form below:








    Leave a Reply

    page counter
    NiharsWorld on Twitter NiharsWorld on Facebook NiharsWorld RSS Feed