Python provides two special types of methods: static methods and class methods. These methods allow us to define behaviors at the class level rather than the instance level.
In this tutorial, we will cover:
@staticmethod and @classmethod decorators| Feature | Instance Method | Class Method | Static Method |
|---|---|---|---|
Requires self? | Yes | No | No |
Requires cls? | No | Yes | No |
| Can access instance variables? | Yes | No | No |
| Can modify class variables? | No | Yes | No |
| Belongs to | Object | Class | Class |
@staticmethod)A static method is a method that does not depend on instance or class variables. It is simply a function that belongs to a class but does not access or modify class or instance attributes.
A static method is defined using the @staticmethod decorator.
class MathOperations:
@staticmethod
def add(a, b):
return a + b
# Calling the static method
print(MathOperations.add(10, 20)) # Output: 30Since add() does not use self or cls, it is a static method.
When a method performs a task independent of class or instance attributes
For utility functions (e.g., math operations, data conversions)
@classmethod)A class method is a method that works on the class level and can access or modify class variables. It takes cls as its first parameter instead of self.
A class method is defined using the @classmethod decorator.
class Employee:
company = "Dynamic Duniya" # Class variable
@classmethod
def set_company(cls, new_name):
cls.company = new_name # Modifying class attribute
# Before modification
print(Employee.company) # Output: Dynamic Duniya
# Changing company name
Employee.set_company("Tech Innovators")
print(Employee.company) # Output: Tech InnovatorsThe class method set_company() modifies the class variable company.
When you need to modify class attributes
For alternative constructors (creating objects in different ways)
class Temperature:
@staticmethod
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
print(Temperature.celsius_to_fahrenheit(25)) # Output: 77.0The method converts temperature and does not use instance or class variables, making it a static method.
Class methods can be used to create objects in multiple ways.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(cls, student_str):
name, age = student_str.split("-")
return cls(name, int(age))
# Creating student object using a string
s1 = Student.from_string("Amit-21")
print(s1.name, s1.age) # Output: Amit 21The from_string() class method allows us to create a Student object from a formatted string.
Static methods (@staticmethod): Do not use self or cls, used for utility functions
Class methods (@classmethod): Use cls, modify class variables, useful for alternative constructors
Instance methods: Use self, operate on individual objects
Sign in to join the discussion and post comments.
Sign inPython for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.
Python Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.