In a Python class, we can define three types of methods:
Let’s discuss them one by one.
Instance methods are the most used methods in a Python class. These methods are only accessible through class objects. If we want to modify any class variable, this should be done inside an instance method.
The first parameter in these methods is self
. self
is used to refer to the current class object’s properties and attributes.
Take a look at the code snippet below to understand this.
class Cricket:teamName = Nonedef setTeamName(self, name):self.teamName = namedef getTeamName(self):return self.teamNamec = Cricket()c.setTeamName('India')print(c.getTeamName())
Explanation
None
.Class methods are usually used to access class variables. You can call these methods directly using the class name instead of creating an object of that class.
To declare a class method, we need to use the @classmethod
decorator. Also, as in the case of instance methods, self
is the keyword used to access the class variables. In class methods, we use use the cls
variable to refer to the class.
Take a look at the code snippet below to understand this concept.
class Cricket:teamName = 'India'@classmethoddef getTeamName(cls):return cls.teamNameprint(Cricket.getTeamName())
Explanation
Cricket
.@classmethod
to specify the below method as a class method.cls
to access the class variable. You can give any name for this parameter, but as per the convention, the name of this parameter should be cls
.Static methods are usually used as a utility function or when we do not want an inherited class to modify a function definition. These methods do not have any relation to the class variables and instance variables; so, are not allowed to modify the class attributes inside a static method.
To declare a static method, we need to use the @staticmethod
. Again, we will be using the cls
variable to refer to the class. These methods can be accessed using the class name as well as class objects.
Take a look at the code snippet below to understand this concept.
class Cricket:teamName = 'India'@staticmethoddef utility():print("This is a static method.")c1 = Cricket()c1.utility()Cricket.utility()
Explanation
@staticmethod
to specify the below method as a static method.