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:
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.
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
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() )
In the above program,
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.
classsmethod() function returns a class method as output for the given function.
Here is the syntax for it:
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:
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())
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.
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())
This a student class
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.
SHARE YOUR THOUGHTS WITH US!