#Day11- Instance Methods, Class Methods, and Static Methods

#Day11- Instance Methods, Class Methods, and Static Methods

Today we will be discussing instance methods, class methods, and static methods in Python. These methods are used with Object-Oriented Programming in Python.

Instance Methods

image.png

Instance methods are the most commonly used methods in OOP in Python. All instance methods are able to change the state of an object, i.e alter the values of the members(variables) of the object. The first parameter is self which basically points to the instance calling the method. The name self is just a naming convention, technically it can be called anything.

drink_1 = Drink("Pepsi","small")
drink_1.get_drink()

In the second line, self points to the instance drink_1. We can also explicitly set the value for the self parameter.

drink_1 = Drink("Pepsi","small")
Drink.get_drink(self = drink_1)

Class Methods

Let's assume we are defining a schema for a User table

image.png

Now let's say we want to create a function that returns all the rows in the table. We could add it as an instance method.

image.png

The above class definition is functionally correct. However, to call the function get_all_rows(), we need an instance of the User class to be created. But what if we want to access this method from the class itself and not the instance. Consider the case when the table is empty or basically, no instance of the class User is created. We should still be able to execute the query.

We can use the classmethod decorator to make the function get_all_rows() a class method and make it callable using the Class itself.

image.png

Now, we can call the function get_all_rows() in the following way

User.get_all_rows()

Like the word self, cls is also a naming convention. In the above code snippet, when the function get_all_rows() is called using the Class User, cls points to the class.

Although get_all_rows() is a class method, it can also be called using an instance of the class. If it is called using an instance, cls points to the instance.

User().get_all_rows()

Class methods can also be used to follow the factory pattern or have multiple constructor functions.

image.png

We have a couple of class methods for the class User. The first method lets us create an instance of User from a JSON object while the second method lets us create an admin user.

Since cls points to the class, cls(name , age) creates an instance of the class. We basically return the instance and this instance is assigned to a variable.

Static Methods

Static Method is similar to a class method, however, it can not access any members of the class or members of an instance. It doesn't accept a parameter like cls or self. It is more of an utility function, i.e a function that doesn't need access to the members of the class but can be used to describe the class in some way. To define a static method, we use the staticmethod decorator

image.png

Consider the static method is_drink_healthy, it doesn't need to access any members of the class or the instance but it does describe the class. Since the function is related to the class, it can be used as a static method associated with the class. It could also be defined outside the class. However, since it can be used to describe the class, it is a good practice to have it as a static method.

To call the function is_drink_helathy, we can call it using the class or an instance

Drink.is_drink_healthy()
Drink().is_drink_healthy()

Combining all the methods together

image.png

  • We have a class called Pizza, the init method accepts a list of ingredients and creates an instance of Pizza
  • It also has two class methods that let us create a veggie or chicken pizza
  • It has a static method to calculate the area of the pizza, the method doesn't need to access any of the members of the class or instance. it is a utility function.

Summary

  • Instance methods are the normal methods defined inside the class. They need an instance to be created to be called. The first parameter is self which points to the instance of the class
  • Class Method can be called using the class itself and do not need an instance to be created. The first parameter is cls which points to the Class if it is called using the class or it could point to an instance if it is called using the instance
  • Static methods are utility functions that can be used to describe the class/instance. They do not have access to the members of the class/instance.