Python- Day 29- CLASSES-Working with Classes and Instances
Classes in object-oriented programming are templates that define attributes and behaviors (methods) for instances.
Instances are unique objects created from a class. By interacting with instances, you can model real-world entities, like cars.
You can modify an instance’s attributes directly or through methods that handle specific changes.
Creating the Car Class:
This class represents a car with attributes such as make
, model
, and year
.
It also includes a method, get_descriptive_name()
, that returns a formatted description of the car.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
my_new_car = Car('audi', 'a4', 2024)
print(my_new_car.get_descriptive_name())
>>
# Output: 2024 Audi A4
Setting a Default Value for an Attribute:
Attributes can be set to default values when instances are created.
For example, odometer_reading
starts at 0
for a new car and can be updated later.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 # Default value
def read_odometer(self):
#Print a statement showing the car's mileage.
print(f"This car has {self.odometer_reading} miles on it.")
my_new_car = Car('audi', 'a4', 2024)
my_new_car.read_odometer()
>>
# Output: This car has 0 miles on it.
Modifying Attribute Values:
There are three ways to modify an attribute’s value: directly, through a method, or by incrementing.
Modifying Directly:
You can change the value of an attribute directly by accessing it.
my_new_car.odometer_reading = 23
my_new_car.read_odometer() # Output: This car has 23 miles on it.
Using a Method to Update the Attribute:
To control how an attribute is updated, you can use a method.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def update_odometer(self, mileage):
#Set the odometer reading to the given value.
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def read_odometer(self):
print(f"This car has {self.odometer_reading} miles on it.")
my_new_car = Car('audi', 'a4', 2024)
my_new_car.update_odometer(23)
my_new_car.read_odometer() # Output: This car has 23 miles on it.
Incrementing an Attribute’s Value:
Sometimes, you want to increase an attribute’s value by a specified amount, rather than set a new value entirely.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def increment_odometer(self, miles):
#Add the given amount to the odometer reading.
self.odometer_reading += miles
my_used_car = Car('subaru', 'outback', 2019)
my_used_car.update_odometer(23_500)
my_used_car.increment_odometer(100)
my_used_car.read_odometer() # Output: This car has 23600 miles on it.
By using classes and instances, you can model real-world objects in code, manage attributes, and define actions through methods.
Methods allow for controlled updates to attributes, improving data integrity and helping prevent accidental changes.
By following these principles, you ensure your code remains maintainable and intuitive for other developers.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!