Tuesday, March 15, 2022

Distinguishing between class methods and instance methods

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

• Instance methods: They are associated with an instance and need an instance to be created first before executing them. They accept the first attribute as a reference to the instance (self) and can read and update the state of the instance. __init__,which is a constructor method, is an example of an instance method.

• Class methods: These methods are declared with the @classmethod decorator. These methods don't need a class instance for execution. For this method, the class reference (cls is used as a convention) will be automatically sent as the first argument.

• Static methods: These methods are declared with the @staticmethod decorator. They don't have access to cls or self objects. Static methods are like utility functions that take certain arguments and provide the output based on the arguments' values—for example, if we need to evaluate certain input data or parse data for processing, we can write static methods to achieve these goals. Static methods work like regular functions that we define in modules but are available in the context of the class's namespace.

To illustrate how these methods can be defined and then used in Python, we created a simple program, which is shown next:

#methodsexample1.py

class Car:

c_mileage_units = "Mi"

def __init__(self, color, miles):

self.i_color = color

self.i_mileage = miles

def print_color (self):

print (f"Color of the car is {self.i_color}")

@classmethod

def print_units(cls):

print (f"mileage unit are {cls.c_mileage_unit}")

print(f"class name is {cls.__name__}")

@staticmethod

def print_hello():

print ("Hello from a static method")

if __name__ == "__main__":

car = Car ("blue", 1000)

car.print_color()

car.print_units()

car.print_hello()

Car.print_color(car);

Car.print_units();

Car.print_hello()

In this program, we did the following:

1. We created a Car class with a class attribute (c_mileage_units), a class method (print_units), a static method (print_hello), instance attributes (i_color and i_mileage), an instance method (print_color), and a constructor method (__init__).

2. We created an instance of the Car class using its constructor as car.

3. Using the instance variable (car in this example), we called the instance method, the class method, and the static method.

4. Using the class name (Car in this example), we again triggered the instance method, the class method, and the static method. Note that we can trigger the instance method using the class name, but we need to pass the instance variable as a first argument (this also explains why we need the self argument for each instance method).

The console output of this program is shown next for reference:

Color of the car is blue

mileage unit are Mi

class name is Car

Hello from a static method

Color of the car is blue

mileage unit are Mi

class name is Car

Hello from a static method

Share:

0 comments:

Post a Comment