Class method and static method in python

Class method and static method in python

In Python, classes can have various types of methods: instance methods, class methods, and static methods. Let's explore class methods and static methods in detail.

1. Class Methods:

  • Defined using the @classmethod decorator.
  • Take a reference to the class (cls by convention) as their first parameter.
  • Can modify class-level attributes, but not instance-specific attributes.
  • Often used as factory methods to create new instances in alternative ways.

Example:

class MyClass: class_attribute = "class value" @classmethod def modify_class_attribute(cls, new_value): cls.class_attribute = new_value # Use class method MyClass.modify_class_attribute("new class value") print(MyClass.class_attribute) # Output: new class value 

2. Static Methods:

  • Defined using the @staticmethod decorator.
  • Do not take any special first parameter like self or cls.
  • Can't modify class-level or instance-specific attributes directly (unless passed as an argument).
  • Often used for utility functions that are related to the class but don't need to access any class or instance-specific data.

Example:

class MyClass: @staticmethod def utility_function(value1, value2): return value1 + value2 # Use static method result = MyClass.utility_function(10, 5) print(result) # Output: 15 

Differences Between Class Methods and Static Methods:

  1. First Parameter:

    • Class Methods: Have cls (reference to the class) as their first parameter.
    • Static Methods: Do not have any special first parameter.
  2. Purpose:

    • Class Methods: Often used for operations that need to access or modify class-specific information.
    • Static Methods: Used for utility tasks that don't need to access or modify the class or instance data.
  3. Decoration:

    • Class Methods: Decorated with @classmethod.
    • Static Methods: Decorated with @staticmethod.

Demonstration with Example:

Let's use a Person class to demonstrate both:

class Person: count = 0 # class attribute to keep track of the number of Person instances def __init__(self, name): self.name = name Person.count += 1 @classmethod def get_instance_count(cls): return cls.count @staticmethod def is_adult(age): return age >= 18 # Create instances p1 = Person("Alice") p2 = Person("Bob") # Use class method print(Person.get_instance_count()) # Output: 2 # Use static method print(Person.is_adult(20)) # Output: True print(Person.is_adult(15)) # Output: False 

Summary:

  • Class Methods:

    • Related to the class but not tightly bound to the instance.
    • Have access to class attributes.
    • Defined using the @classmethod decorator.
  • Static Methods:

    • Don't have access to class or instance attributes unless they are passed as parameters.
    • More like regular functions but are bound to the class's namespace.
    • Defined using the @staticmethod decorator.

Understanding when and how to use class methods and static methods is important for creating well-structured object-oriented Python code.


More Tags

html5-video ipython google-api-python-client entity-framework-4.1 jtextarea data-visualization watson-nlu credentials build-tools frame-rate

More Programming Guides

Other Guides

More Programming Examples