Python

 

 

 

Introduction

In Python, methods can be categorized into three types: regular methods, class methods, and static methods. Understanding how and when to use class methods and static methods is crucial for writing clean, efficient code. In this tutorial, we will cover how to define and use class methods and static methods in Python.

Objective

By the end of this tutorial, you’ll have a clear understanding of:

  • What class methods and static methods are.
  • How to define and use them in Python.
  • The differences between regular methods, class methods, and static methods.

Python Code Example


class MyClass:
    # Regular method
    def regular_method(self, name):
        return f"Hello, {name}!"

    # Class method
    @classmethod
    def class_method(cls, class_name):
        return f"This is a class method of {class_name}"

    # Static method
    @staticmethod
    def static_method():
        return "This is a static method, no access to class or instance."
    
# Creating an instance of MyClass
obj = MyClass()

# Using the regular method
print(obj.regular_method("John"))

# Using the class method
print(MyClass.class_method("MyClass"))

# Using the static method
print(MyClass.static_method())

Explanation of the Program

The code above demonstrates the following:

  • Regular Method: A method that requires an instance of the class to be called. It has access to the instance and class attributes.
  • Class Method: A method that is bound to the class rather than the instance. It is defined using the @classmethod decorator. It takes the class as its first argument (conventionally named cls).
  • Static Method: A method that doesn’t have access to the instance or class. It is defined using the @staticmethod decorator and is typically used for utility functions that don’t need access to the class or instance.

How to Run the Program:

  1. Save the code in a Python file (e.g., example.py).
  2. Open your terminal or command prompt.
  3. Navigate to the directory where your Python file is saved.
  4. Run the command python example.py.
  5. You should see the output printed for all three methods:

Hello, John!
This is a class method of MyClass
This is a static method, no access to class or instance.

 

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)