Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Magic Methods
0/1
Python
About Lesson

A static method in Python is a method that is bound to the class rather than to an instance of the class. It doesn’t have access to the instance (self) or the class (cls). Static methods are defined using the @staticmethod decorator, and they are typically used when the method does not need to access or modify the instance or class-specific data.

Static Members in Python

Static members can be of two types:

  1. Static Variables (Class Variables)
  2. Static Methods

1. Static Variables (Class Variables)

A static variable (or class variable) is a variable that is shared across all instances of a class. It belongs to the class itself rather than to any specific instance. These variables are defined directly in the class and not within any method.

Example of Static Variables (Class Variables):

Python
class Car:
    # Static variable (class variable)
    wheels = 4  # All cars have 4 wheels by default

    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

# Accessing the static variable using an instance
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")

print(car1.wheels)  # Output: 4
print(car2.wheels)  # Output: 4

# Accessing the static variable using the class itself
print(Car.wheels)  # Output: 4

Key Points:

  • Static variables are shared by all instances of the class. If you modify the value of the static variable through one instance, the change will be reflected in all instances.
  • Static variables are typically used for values that should be the same for every instance of the class (e.g., default settings, constants, etc.).

2. Static Methods

A static method is a method that is bound to the class rather than any instance of the class. Static methods do not have access to the instance (self) or class (cls) variables. They are typically used for utility functions that don’t need to access any instance-specific data.

To define a static method in Python, you use the @staticmethod decorator.

Example of Static Methods:

Python
class MathOperations:
    @staticmethod
    def add(x, y):
        return x + y
    
    @staticmethod
    def multiply(x, y):
        return x * y

# Using static methods without creating an instance
print(MathOperations.add(10, 20))  # Output: 30
print(MathOperations.multiply(10, 20))  # Output: 200

# Static methods can also be called on instances, though it's less common
math_instance = MathOperations()
print(math_instance.add(10, 20))  # Output: 30

Key Points:

  • Static methods do not take the self or cls parameter, meaning they cannot access or modify instance or class variables directly.
  • They can be called using the class name or an instance, but are often used when the method logic does not depend on the instance state.
  • Static methods are used when you need functionality that operates independently of any object or class state. For example, mathematical operations, helper functions, or utility functions.

Example with Both Static Variables and Static Methods:

Python
class Dog:
    # Static variable (class variable)
    species = "Canis familiaris"  # All dogs are of species "Canis familiaris"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Static method to display a common message for all dogs
    @staticmethod
    def common_message():
        print("Dogs are loyal and friendly animals!")

# Accessing static variable (class variable)
print(Dog.species)  # Output: Canis familiaris

# Calling static method
Dog.common_message()  # Output: Dogs are loyal and friendly animals!

# Create instances
dog1 = Dog("Buddy", 5)
dog2 = Dog("Max", 3)

# Static variable is shared among all instances
print(dog1.species)  # Output: Canis familiaris
print(dog2.species)  # Output: Canis familiaris

# Static method can also be called on instances
dog1.common_message()  # Output: Dogs are loyal and friendly animals!