PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » Python Object-Oriented Programming (OOP) » Python Class Method vs. Static Method vs. Instance Method

Python Class Method vs. Static Method vs. Instance Method

Updated on: August 28, 2021 | 2 Comments

In this tutorial, you’ll understand the difference between class method vs. static method vs. instance method step by step.

In Object-oriented programming, when we design a class, we use the following three methods

  • Instance method performs a set of actions on the data/value provided by the instance variables. If we use instance variables inside a method, such methods are called instance methods.
  • Class method is method that is called on the class itself, not on a specific object instance. Therefore, it belongs to a class level, and all class instances share a class method.
  • Static method is a general utility method that performs a task in isolation. This method doesn’t have access to the instance and class variable.
Python class method vs static method vs instance method
class method vs static method vs instance method

Table of contents

  • Difference #1: Primary Use
  • Difference #2: Method Defination
  • Difference #3: Method Call
  • Difference #4: Attribute Access
  • Difference #5: Class Bound and Instance Bound

Difference #1: Primary Use

  • Class method Used to access or modify the class state. It can modify the class state by changing the value of a class variable that would apply across all the class objects.
  • The instance method acts on an object’s attributes. It can modify the object state by changing the value of instance variables.
  • Static methods have limited use because they don’t have access to the attributes of an object (instance variables) and class attributes (class variables). However, they can be helpful in utility such as conversion form one type to another.

Class methods are used as a factory method. Factory methods are those methods that return a class object for different use cases. For example, you need to do some pre-processing on the provided data before creating an object.

Read our separate tutorial on

  • Instance methods in Python
  • Class method in Python
  • Static method in Python

Difference #2: Method Defination

Let’s learn how to define instance method, class method, and static method in a class. All three methods are defined in different ways.

  • All three methods are defined inside a class, and it is pretty similar to defining a regular function.
  • Any method we create in a class will automatically be created as an instance method. We must explicitly tell Python that it is a class method or static method.
  • Use the @classmethod decorator or the classmethod() function to define the class method
  • Use the @staticmethod decorator or the staticmethod() function to define a static method.

Example:

  • Use self as the first parameter in the instance method when defining it. The self parameter refers to the current object.
  • On the other hand, Use cls as the first parameter in the class method when defining it. The cls refers to the class.
  • A static method doesn’t take instance or class as a parameter because they don’t have access to the instance variables and class variables.
class Student:
    # class variables
    school_name = 'ABC School'

    # constructor
    def __init__(self, name, age):
        # instance variables
        self.name = name
        self.age = age

    # instance variables
    def show(self):
        print(self.name, self.age, Student.school_name)

    @classmethod
    def change_School(cls, name):
        cls.school_name = name

    @staticmethod
    def find_notes(subject_name):
        return ['chapter 1', 'chapter 2', 'chapter 3']

As you can see in the example, in the instance

Difference #3: Method Call

  • Class methods and static methods can be called using ClassName or by using a class object.
  • The Instance method can be called only using the object of the class.

Example:

# create object
jessa = Student('Jessa', 12)
# call instance method
jessa.show()

# call class method using the class
Student.change_School('XYZ School')
# call class method using the object
jessa.change_School('PQR School')

# call static method using the class
Student.find_notes('Math')
# call class method using the object
jessa.find_notes('Math')

Output:

Jessa 12 ABC School
School name changed to XYZ School
School name changed to PQR School

Difference #4: Attribute Access

Both class and object have attributes. Class attributes include class variables, and object attributes include instance variables.

  • The instance method can access both class level and object attributes. Therefore, It can modify the object state.
  • Class methods can only access class level attributes. Therefore, It can modify the class state.
  • A static method doesn’t have access to the class attribute and instance attributes. Therefore, it cannot modify the class or object state.

Example:

class Student:
    # class variables
    school_name = 'ABC School'

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

    # instance method
    def show(self):
        # access instance variables
        print('Student:', self.name, self.age)
        # access class variables
        print('School:', self.school_name)

    @classmethod
    def change_School(cls, name):
        # access class variable
        print('Previous School name:', cls.school_name)
        cls.school_name = name
        print('School name changed to', Student.school_name)

    @staticmethod
    def find_notes(subject_name):
        # can't access instance or class attributes
        return ['chapter 1', 'chapter 2', 'chapter 3']

# create object
jessa = Student('Jessa', 12)
# call instance method
jessa.show()

# call class method
Student.change_School('XYZ School')

Output:

Student: Jessa 12
School: ABC School
Previous School name: ABC School
School name changed to XYZ School

Difference #5: Class Bound and Instance Bound

  • An instance method is bound to the object, so we can access them using the object of the class.
  • Class methods and static methods are bound to the class. So we should access them using the class name.

Example:

class Student:
    def __init__(self, roll_no): self.roll_no = roll_no

    # instance method
    def show(self):
        print('In Instance method')

    @classmethod
    def change_school(cls, name):
        print('In class method')

    @staticmethod
    def find_notes(subject_name):
        print('In Static method')

# create two objects
jessa = Student(12)

# instance method bound to object
print(jessa.show)

# class method bound to class
print(jessa.change_school)

# static method bound to class
print(jessa.find_notes)

Do you know:

In Python, a separate copy of the instance methods will be created for every object.

Suppose you create five Student objects, then Python has to create five copies of the show() method (separate for each object). So it will consume more memory. On the other hand, the static method has only one copy per class.

Example:

# create two objects
jessa = Student(12)
kelly = Student(25)

# False because two separate copies
print(jessa.show is kelly.show)

# True objects share same copies of static methods
print(jessa.find_notes is kelly.find_notes)
Jessa 20 ABC School
Jessa 20 XYZ School
<bound method Student.change_School of <class '__main__.Student'>>

As you can see in the output, the change_School() method is bound to the class.

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2022 pynative.com