Different types of methods that can be defined in a Python class

In a Python class, we can define three types of methods:

  • Instance methods
  • Class methods
  • Static methods

Let’s discuss them one by one.

Instance methods

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 = None
def setTeamName(self, name):
self.teamName = name
def getTeamName(self):
return self.teamName
c = Cricket()
c.setTeamName('India')
print(c.getTeamName())

Explanation

  • In line 1, we define our class.
  • In line 2, we define a class variable and set it to None.
  • In lines 4 and 7, we create two instance methods: setTeamName()will set the value of the class variable and getTeamName()will return the value of the class variable.
  • In lines 11 and 12, we use the class object to access the instance methods.

Class methods

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'
@classmethod
def getTeamName(cls):
return cls.teamName
print(Cricket.getTeamName())

Explanation

  • In line 1, we create our class Cricket.
  • In line 2, we define a class variable.
  • In line 4, we use the decorator @classmethod to specify the below method as a class method.
  • In line 5, we define our class method. (Note that we have used 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.
  • In line 8, we call our class method by using the class name instead of creating an object of the class.

Static methods

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'
@staticmethod
def utility():
print("This is a static method.")
c1 = Cricket()
c1.utility()
Cricket.utility()

Explanation

  • The code is almost the same, but with a difference in line 4, where we used the decorator @staticmethod to specify the below method as a static method.
  • Then, in line 9, we call our static method by using the class object and, in line 11, we call the static method using the class name.

Free Resources