Google News
logo
Python Program to Modify object properties
In the following example of Python program that demonstrates how to modify object properties :
Program :
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def get_info(self):
        return f"{self.make} {self.model} ({self.year})"

# Create a Car object
my_car = Car("Toyota", "Corolla", 2020)

# Print the current make and model
print(my_car.get_info())  # Output: Toyota Corolla (2020)

# Change the make of the car
my_car.make = "Honda"

# Print the updated make and model
print(my_car.get_info())  # Output: Honda Corolla (2020)
In this example, we first define a `Car` class with an `__init__()` method that initializes the make, model, and year properties of a car object.

We also define a `get_info()` method that returns a string containing the make, model, and year of the car.