Class Method vs Static Method in Python

1. Introduction

In Python, class methods and static methods are two types of methods that can be associated with a class rather than with instances of the class. A class method takes the class itself as its first argument and can access and modify the class state. A static method, on the other hand, doesn't take any specific first argument (neither the instance nor the class), and it behaves just like a regular function but belongs to the class's namespace.

2. Key Points

1. First Argument: Class methods take the class (cls) as the first argument and static methods don’t take an implicit first argument.

2. Decorator: Class methods use the @classmethod decorator and static methods use @staticmethod.

3. Access to Class State: Class methods can access and modify class state, static methods can’t.

4. Usage: Class methods are used for factory methods and static methods for utility functions.

3. Differences

Aspect Class Method Static Method
First Argument Class (cls) None
Decorator @classmethod @staticmethod
Access to Class State Yes No
Usage Factory methods Utility functions

4. Example

class MyClass:
    @classmethod
    def class_method(cls):
        return 'called class_method', cls

    @staticmethod
    def static_method():
        return 'called static_method'

# Create instance of MyClass
my_instance = MyClass()

# Call class method
class_method_output = my_instance.class_method()

# Call static method
static_method_output = my_instance.static_method()

Output:

Class Method Output:
('called class_method', <class '__main__.MyClass'>)
Static Method Output:
called static_method

Explanation:

1. The class_method accessed the class (cls) and returned its name along with a message.

2. The static_method worked independently of any class or instance information.

5. When to use?

- Use class methods when you need to have a method that is aware of its class and can access its properties or methods.

- Use static methods when you want to place a method inside a class for organization, but the method doesn’t need to access any properties of the class or of an instance.

Related Python Posts:

Difference Between Local and Global Variables in Python

Difference Between List and Tuple in Python

Difference Between Array and List in Python

Difference Between List and Dictionary in Python

Difference Between List, Tuple, Set and Dictionary in Python

Difference Between a Set and Dictionary in Python

Difference between for loop and while loop in Python

Difference Between pass and continue in Python

Difference Between List append and extend in Python

Difference Between == and is operator in Python

Difference Between Deep and Shallow Copy in Python

Class Method vs Static Method in Python

Class Method vs Instance Method in Python

Difference Between List and Set in Python

Difference Between Generator and Iterator in Python

Difference Between str and repr in Python

Method Overloading vs Overriding in Python

Difference Between Dictionary and Tuple in Python

Difference Between Dictionary and Object in Python

Difference Between Mutable and Immutable in Python

Difference Between Interface and Abstract Class in Python

Difference Between Python Script and Module

Difference Between for Loop and Iterator in Python

Comments