machine-learning-articles icon indicating copy to clipboard operation
machine-learning-articles copied to clipboard

The definitive guide on how to use static, class or abstract methods in Python

Open khuyentran1401 opened this issue 4 years ago • 0 comments

TL;DR

Difference between static and class method for class

Article Link

https://julien.danjou.info/guide-python-static-class-abstract-methods/

Key Takeaways

  • @staticmethod and @classmethod are useful when we don't want to instantiate the class
  • @staticmethod does not use attributes or methods of the class
  • @staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. Its definition is immutable via inheritance.
  • @staticmethods should not rely on attributes which require initialization of the object itself
  • @classmethod uses attributes or methods of the class but does not need the class to be instantiated.
  • @classmethod is especially useful when you move your function to other class, you don't have to rename the class reference
  • Even though @staticmethod can access to other members of the class, you need to repeat the class (i.e, Person.method1()) while @classmethod does call the method with cls.method1()

khuyentran1401 avatar Aug 15 '20 01:08 khuyentran1401