Python - OOP - Object Oriented Programming - Part 4

In Python, the self parameter refers to the instance of the class. It is used to access the instance variables and methods of the class. When we create an object of a class, we pass it as the first parameter to the method of the class. The name self is just a convention and can be changed to any other name.

Here's an example of how self is used in Python:

class MyClass:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def display(self):
        print("Name:", self.name)
        print("Age:", self.age)
obj = MyClass("John", 25)
obj.display()

In this example, we have a class MyClass with two instance variables name and age. The __init__ method is a constructor that is called when we create an object of the class. It initializes the instance variables with the values passed to it.

The display method is another method of the class that takes the self parameter. This method is used to display the values of the instance variables. We create an object obj of the class MyClass with the name "John" and age 25. We then call the display method of the object obj using the dot notation. The self parameter is automatically passed to the method and we can access the instance variables of the object using it.

To modify the properties of an object in Python, you can simply access the property using the dot notation and assign a new value to it. Here's an example:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
       
    def start_engine(self):
        print("Starting the engine...")

    def stop_engine(self):
        print("Stopping the engine...")

# create an object of Car class
my_car = Car("Toyota", "Camry", 2020)

# access and modify object properties
my_car.year = 2022
my_car.start_engine()

# output: 2022
print(my_car.year)

In this example, we create an object of the Car class and assign it to the variable my_car. We then access and modify the year property of the object by using the dot notation and assigning a new value to it. Finally, we call the start_engine() method of the object to start the engine.

In Python, you can delete object properties using the del statement. The del statement is used to delete a specific object property or an entire object.

Here's an example of deleting an object property:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
my_car = Car("Honda", "Civic", 2019)
# Deleting a single property
del my_car.year
print(my_car.make)   # Output: Honda
print(my_car.model)  # Output: Civic

In the above example, we create a Car class with three properties make, model, and year. Then we create an instance of the Car class called my_car and assign it some values.

To delete a single property, we use the del statement followed by the object name and the name of the property we want to delete, i.e., del my_car.year. After deleting the year property, we can still access the make and model properties.

Here's an example of deleting an entire object:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
my_car = Car("Honda", "Civic", 2019)
# Deleting the entire object
del my_car

print(my_car)  # Output: NameError: name 'my_car' is not defined

In the above example, we create a Car class with three properties make, model, and year. Then we create an instance of the Car class called my_car and assign it some values.

To delete the entire object, we use the del statement followed by the object name, i.e., del my_car. After deleting the entire my_car object, we can no longer access it and trying to print it will result in a NameError.

The pass statement in Python is a null statement, which means that it does nothing. It is often used as a placeholder or as a stub for unfinished code.

The pass statement is commonly used in situations where the syntax requires a statement, but the program logic does not. For example, when defining a class or function that does not have any code yet, we can use the pass statement to indicate that the body of the class or function is empty:

class MyClass:
    pass
def my_function():
    pass

In the above code, MyClass and my_function are both defined, but their bodies are empty because they do not have any code to execute. The pass statement is used to satisfy the syntax requirement of having a valid class or function definition, while allowing us to defer writing the actual code until later.

The pass statement can also be used as a placeholder in conditional statements, loops, and exception handling. For example, in the case of an if statement where we want to do nothing if a condition is true, we can use the pass statement:

if x > 0:
    # do something
else:
    pass   # do nothing

In the above code, if the condition x > 0 is false, the else block is executed, which contains the pass statement. The pass statement does nothing, so the program simply moves on to the next statement after the else block.