Signup/Sign In
LAST UPDATED: MARCH 23, 2023

Types Of Methods In Python - Instance Method, Class Method, and Static Method

Technology

    Hello Geeks, in this tutorial we are going to have a brief look at types of methods in python. Generally, there are three types of methods in Python:

    1. Instance Methods.

    2. Class Methods

    3. Static Methods

    Types of methods in python

    Before moving on with the topic, we have to know some key concepts.

    Class Variable: A class variable is nothing but a variable that is defined outside the constructor. A class variable is also called as a static variable.

    Accessor(Getters): If you want to fetch the value from an instance variable we call them accessors.

    Mutator(Setters): If you want to modify the value we call them mutators.


    1. Instance Method

    This is a very basic and easy method that we use regularly when we create classes in python. If we want to print an instance variable or instance method we must create an object of that required class.

    If we are using self as a function parameter or in front of a variable, that is nothing but the calling instance itself.

    As we are working with instance variables we use self keyword.

    Note: Instance variables are used with instance methods.

    Look at the code below

    # Instance Method Example in Python 
    class Student:
        
        def __init__(self, a, b):
            self.a = a
            self.b = b 
        
        def avg(self):
            return (self.a + self.b) / 2
    
    s1 = Student(10, 20)
    print( s1.avg() )

    Output:

    15.0

    In the above program, a and b are instance variables and these get initialized when we create an object for the Student class. If we want to call avg() function which is an instance method, we must create an object for the class.

    If we clearly look at the program, the self keyword is used so that we can easily say that those are instance variables and methods.


    2. Class Method

    classsmethod() function returns a class method as output for the given function.

    Here is the syntax for it:

    classmethod(function)

    The classmethod() method takes only a function as an input parameter and converts that into a class method.

    There are two ways to create class methods in python:

    1. Using classmethod(function)

    2. Using @classmethod annotation

    A class method can be called either using the class (such as C.f()) or using an instance (such as C().f()). The instance is ignored except for its class. If a class method is called from a derived class, the derived class object is passed as the implied first argument.

    As we are working with ClassMethod we use the cls keyword. Class variables are used with class methods.

    Look at the code below.

    # Class Method Implementation in python 
    class Student:
        name = 'Student'
        def __init__(self, a, b):
            self.a = a
            self.b = b 
        
        @classmethod
        def info(cls):
            return cls.name
    
    print(Student.info())

    Output:

    Student

    In the above example, name is a class variable. If we want to create a class method we must use @classmethod decorator and cls as a parameter for that function.


    3. Static Method

    A static method can be called without an object for that class, using the class name directly. If you want to do something extra with a class we use static methods.

    For example, If you want to print factorial of a number then we don't need to use class variables or instance variables to print the factorial of a number. We just simply pass a number to the static method that we have created and it returns the factorial.

    Look at the below code

    # Static Method Implementation in python
    class Student:
        name = 'Student'
        def __init__(self, a, b):
            self.a = a
            self.b = b 
        
        @staticmethod
        def info():
            return "This is a student class"
    
    print(Student.info())
    

    Output

    This a student class
    


    Conclusion

    Pheww! This is a brief explanation of the types of methods in python. I hope that you enjoyed the post and learned about the types of methods in python. If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.


    Frequently Asked Questions

    1. What are the different types of methods in Python?

    The three main types are instance methods, class methods, and static methods. Each has unique features and use cases.

    2. What is an instance method in Python?

    An instance method is a method that is defined within a class and is called on an instance of that class. It takes the instance as the first argument.

    3. What is a class method in Python?

    A class method is a method that is defined within a class and is called on the class itself. It takes the class as the first argument and can be used for class-level operations.

    4. What is a static method in Python?

    A static method is a method that is defined within a class but does not depend on any instance or class variables. It can be called on the class itself or on an instance.

    You may also like:

    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS