[Python] Difference between class method and static method

class Parent(object):

    class_var = 'parent_class_var'

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

    def static_method(raw_str):

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

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

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):

    def static_method(raw_str):

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.


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 exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Class method static method
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Class methods and static methods
Python class variables and instance variables
Difference between return, return None, and no return description in Python
About class methods and static methods
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
perl objects and python class part 1.
Difference between SQLAlchemy flush () and commit ()
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)
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
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between numpy.ndarray and list (dimension, size)
Correspondence between Python built-in functions and Rust
Difference between ls -l and cat command
Exchange encrypted data between Python and C #
Talking about Python class attributes and metaclasses
Difference and compatibility verification between keras and tf.keras # 1
Trouble with Python pseudo-private variables and class inheritance
Python class, instance
Johnson method (python)
[Python] Class type and usage of datetime module
#Python basics (class)
About the difference between PostgreSQL su and sudo
[Python] Semi-Lagrange method
What is the difference between Unix and Linux?
[Python] Explain the difference between strftime and strptime in the datetime module with an example
[Memo] Difference between test_split and cross-validation method when verifying generalized performance in deep learning
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
Mutual conversion between JSON and YAML / TOML in Python
Solving one-dimensional wave equation by finite difference method (python)
How to use argparse and the difference between optparse
Interprocess communication between Ruby and Python (POSIX message queue)
Automated testing method combining Beautiful Soup and Selenium (Python)
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Sample of getting module name and class name in Python
[Python] Difference between function and method
[Python] Difference between class method and static method
Difference between numpy.ndarray and list (dimension, size)
Bayesian modeling-estimation of the difference between the two groups-
Summary of test method
Test automation for work
List concatenation method in python, difference between list.extend () and “+” operator
parallelization of class method
Python and Ruby split
python subprocess wrapper class
Simplex method (simplex method) in Python
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
Private method in python
Python asyncio and ContextVar
YOLO Python wrapper class
Class notation in Python
Python exception class list