Sunday, March 13, 2022

Classes and objects

Introducing classes and objects

A class is a blueprint for how something should be defined. It doesn't actually contain any data—it is a template that is used to create instances as per the specifications defined in a template or a blueprint.

An object of a class is an instance that is built from a class, and that is why it is also called an instance of a class. Objects in OOP are occasionally represented by physical objects such as tables, chairs, or books. On most occasions, the objects in a software program represent abstracted entities that may not be physical, such as accounts, names, addresses,and payments.

To refresh ourselves with basic concepts of classes and objects, I will define these terminologies with code examples.

Distinguishing between class attributes and instance attributes

Class attributes are defined as part of the class definition, and their values are meant to be the same across all instances created from that class. The class attributes can be accessed using the class name or instance name, although it is recommended to use a class name to access these attributes (for reading or updating). The state or data of an object is provided by instance attributes.

Defining a class in Python is simply done by using the class keyword. The following code snippet creates a Car class:

#carexample1.py

class Car:

    pass

This class has no attributes and methods. It is an empty class, and you may think this class is useless until we add more components to it. Not exactly! In Python, you can add attributes on the fly without defining them in the class. The following snippet is a valid example of code in which we add attributes to a class instance at runtime:

#carexample1.py

class Car:

    pass

if __name__ == "__main__":

    car = Car ()

    car.color = "blue"

    car.miles = 1000

    print (car.color)

    print (car.miles)

In this extended example, we created an instance (car) of our Car class and then added two attributes to this instance: color and miles. Note that the attributes added using this approach are instance attributes.

Next, we will add class attributes and instance attributes using a constructor method (__init__), which is loaded at the time of object creation. A code snippet with two instance attributes (color and miles) and the init method is shown next:

#carexample2.py

class Car:

    c_mileage_units = "Mi"

    def __init__(self, color, miles):

        self.i_color = color

        self.i_mileage = miles

if __name__ == "__main__":

    car1 = Car ("blue", 1000)

    print (car.i_color)

    print (car.i_mileage)

    print (car.c_mileage_units)

    print (Car.c_mileage_units)

In this program, we did the following:

1. We created a Car class with a c_mileage_units class attribute and two instance variables, i_color and i_mileage.

2. We created an instance (car) of the Car class.

3. We printed out the instance attributes using the car instance variable.

4. We printed out the class attribute using the car instance variable as well as the Car class name. The console output is the same for both cases.

We can update the class attributes using an instance variable or class name, but the outcome can be different. When we update a class attribute using the class name, it is updated for all the instances of that class. But if we update a class attribute using an instance variable, it will be updated only for that particular instance. This is demonstrated in the following code snippet, which is using the Car class:

#carexample3.py

#class definition of Class Car is same as in carexample2.py

if __name__ == "__main__":

car1 = Car ("blue", 1000)

car2 = Car("red", 2000)

print("using car1: " + car1.c_mileage_units)

print("using car2: " + car2.c_mileage_units)

print("using Class: " + Car.c_mileage_units)

car1.c_mileage_units = "km"

print("using car1: " + car1.c_mileage_units)

print("using car2: " + car2.c_mileage_units)

print("using Class: " + Car.c_mileage_units)

Car.c_mileage_units = "NP"

print("using car1: " + car1.c_mileage_units)

print("using car2: " + car2.c_mileage_units)

print("using Class: " + Car.c_mileage_units)

The console output of this program can be analyzed as follows:

1. The first set of print statements will output the default value of the class attribute, which is Mi.

2. After executing the car1.c_mileage_units = "km" statement, the value of the class attribute will be the same (Mi) for the car2 instance and the class-level attribute.

3. After executing the Car.c_mileage_units = "NP" statement, the value of the class attribute for car2 and the class level will change to NP, but it will stay the same (km) for car1 as it was explicitly set by us.

Share:

0 comments:

Post a Comment