[Python] Difference between class method and static method

class Parent(object):

    class_var = 'parent_class_var'

    @classmethod
    def class_method(cls):
        print('cls: {}'.format(cls))
        print("class_var: {}".format(cls.class_var))

    @staticmethod
    def static_method(raw_str):
        print(raw_str.strip())

if __name__ == '__main__':
    raw_str = 'He has been fired\n'
    # cls: <class '__main__.Parent'>
    # class_var: parent_class_var
    Parent.class_method()
    # He has been fired
    Parent.static_method(raw_str)

    parent = Parent()
    # cls: <class '__main__.Parent'>
    # class_var: parent_class_var
    parent.class_method()
    # He has been fired
    parent.static_method(raw_str)

Common point

the difference

How to use properly?

Because static methods don't take classes as arguments Class-independent logic will be implemented as a method. However, if the method does not depend on the class in the first place, shouldn't it be implemented as a function instead of as a method in the class?

class Child(Parent):

    @staticmethod
    def static_method(raw_str):
        print(raw_str.strip()+'!')

However, static methods are useful when inheriting a class and changing the logic content between parent and child. It may be implemented as a class method, It can be emphasized that it is a class-independent process.

Question

Since only static methods exist in C ++ and Java It is undeniable that the existence of both class methods and static methods in Python is somewhat redundant.

Recommended Posts

[Python] Difference between class method and static method
[Python] Difference between function and method
[python] Difference between variables and self. Variables in class
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between python2 series and python3 series dict.keys ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Class method static method
List concatenation method in python, difference between list.extend () and β€œ+” operator
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Class methods and static methods
About the difference between "==" and "is" in python
Difference between process and job
Various class methods and static methods
Python: Class and instance variables
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between Ruby and Python in terms of variables
Difference between regression and classification
Python class variables and instance variables
Difference between return, return None, and no return description in Python
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference in writing method to read external source code between Ruby and Python
Difference between ps a and ps -a
Difference between return and print-Python
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Use instance method and class method properly
perl objects and python class part 2.
Difference between SQLAlchemy filter () and filter_by ()
Memorandum (difference between csv.reader and csv.dictreader)
Python class definitions and instance handling
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between sort and sorted (memorial)
perl objects and python class part 1.
Difference between SQLAlchemy flush () and commit ()
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
File open function in Python3 (difference between open and codecs.open and speed comparison)
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
[Xg boost] Difference between softmax and softprob
[Django ORM] Difference between values () and only ()
Test method for size difference between groups
Difference between linear regression, Ridge regression and Lasso regression
Python basic operation 3rd: Object-oriented and class
Difference between docker-compose env_file and .env file
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
speed difference between wsgi, Bottle and Flask