Search Here

Python Types of Methods Instance Class and Static Methods for Beginners

Python Types of Methods | Instance, Class and Static Methods for Beginners

In this post, we’ll learn about Python Types of Methods | Instance, Class, and Static Methods for Beginners.

If you ever programmed or written a piece of code then you would have definitely used class and defined methods inside the class.
While going through the documentation you would have come across different looking @ symbol and you may have thought. Oh! what is that and what does it too.
Well, then we are here to provide you with an answer to that. They are called decorators they can be built-in or user-defined.

The @ symbol is also used to tell python the type of method your using in class. They are three types of methods as follows:

  • Instance Methods: This method refers to that particular instance, not the class. The instance method can only be called by Class instance.
  • Class Methods: This method can be called through class or instance and is common to any instance of that class.
  • Static Methods: If we want to work with any variables other than class for that we can use the static method.

Table of Contents

Instance Methods

Let us look at the below example.

class Teacher:

    def __init__(self, name, age, course):
        self.name = name
        self.age = age
        self.course = course
    
    def show(self):
        return '''
            name   : {}\n  
            course : {}\n  
            age    : {}\n  
        '''.format(name, course, age)

t1 = Teacher("Suresh", "28", "Maths") #passing instance variables

print(t1.show()) #show is a instance method
Calling show() method by creating instance of object in Python

Calling show() method by creating an instance of an object in Python

The Class Teacher takes three are arguments they are called instance argument/variables. The method show displays the teacher’s information such as name, course, and age.
We have created an instance t1 of class Teacher and through instance t1 we can call method t1.show().
The method show() the result is only attached to that particular instance. The output will change depending upon the instance not the class.

Class Methods

To make the method a class method use decorator @classmethod.

class Teacher:
    
    institute_name = "ABC Institute of Technology"

    def __init__(self, name, age, course):
        self.name = name
        self.age = age
        self.course = course
    
    def show(self):
        return '''
            name   : {}\n  
            course : {}\n  
            age    : {}\n  
        '''.format(name, course, age)

    @classmethod
    def get_institute(cls):
        return cls.institute_name

t1 = Teacher("Kiran", "35", "Science")
print(t1.get_institute()) #Call classmethod using instance t1
print(Teacher.get_institute()) #Call classmethod using class Teacher
Calling get_institute() method by class in Python

Calling get_institute() method by class in Python

Since all teachers work under the same institute that means the institute_name will be the same. So we have declared it in class and it is called a class variable.
The method get_institute(cls) always takes cls is the first argument. It points to that particular class.

@classmethod
def set_institute(cls, name):
    cls.institute_name = name
    return cls.institute_name

t1 = Teacher("Kiran", "35", "Science")
t2 = Teacher("Sunil", "42", "Hindi")

Teacher.set_institute("XYZ institute")

print(t1.get_institute()) #Call classmethod using instance t1
print(t2.get_institute()) #Call classmethod using instance t2
print(Teacher.get_institute()) #Call classmethod using class Teacher

#PYTHON OUTPUT
XYZ institute
XYZ institute
XYZ institute

In the above example the when we update the institute name with XYZ institute by calling a class method Teacher.set_institute() then it changes for all including instance and class.

Static Methods

We can use the static methods in those cases where we need to do some processing that is not related to the class. For example, available_courses is a list that contains courses that are available at the institute.
We have also defined a method check_is_course_available which takes the course as an argument and return boolean True if the course is available else returns False.
The Decorator @staticmethod indicates that it is a static method.

available_courses = [
    "python", "electronics", "cs"
]

class Teacher:
    
    def __init__(self, name, age, course):
        self.name = name
        self.age = age
        self.course = course
        
    @staticmethod
    def check_is_course_available(course):
        is_course_available = False
        
        if course in available_courses:
            is_course_available = True

        return is_course_available

t1 = Teacher("Kiran", "35", "science")
t2 = Teacher("Sunil", "42", "english")

print("\nIs Computer Science course available in institute : {}".format(Teacher.check_is_course_available("cs"))) 
print("\nIs Web Development course available in institute : {}".format(Teacher.check_is_course_available("web_development"))) 
Calling check_is_course_available() method using static method decorator

Calling check_is_course_available() method using static method decorator

Conclusion

You have reached the end of our post on Python Types of Methods | Instance, Class, and Static Methods for Beginners. If you find anything difficult with our way of explanation leave us a comment.

Related Posts

Summary
Review Date
Reviewed Item
Python Types of Methods | Instance, Class and Static Methods for Beginners
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Langauage