๐๏ธObject-Oriented Programming & Sports Cars ๐๏ธ#
๐๏ธ Classes: The Blueprint of High-Speed Beasts#
A class in Python acts like the design blueprint of a sports car. It defines what every sports car should have (attributes) and what it can do (methods).
Example: The SportsCar Class#
class SportsCar:
"""A blueprint for creating sports cars."""
# Class Attribute (shared by all instances)
category = "Sports Car"
def __init__(self, brand, model, horsepower, top_speed):
"""Each sports car has unique specifications."""
self.brand = brand
self.model = model
self.horsepower = horsepower
self.top_speed = top_speed
def display_info(self):
"""Showcase the car's specs."""
return f"๐ฅ {self.brand} {self.model} | HP: {self.horsepower} | Top Speed: {self.top_speed} km/h ๐"
Key Concept:#
A class acts as a template or blueprint for multiple objects.
How do you make a class in Python?#
When you create a class you instantiate it to create an object. An object is an instance of a class. The instantiation process is handeled by the __init__
method. This is a built-in magic method that is called when an object is created.
๐๏ธ Instance Attributes: Making Each Car Unique#
Instance attributes belong to individual objects, meaning each sports car has its own brand, model, horsepower, and top speed.
Example: Creating Car Objects#
car1 = SportsCar("Ferrari", "F8 Tributo", 710, 340)
car2 = SportsCar("Lamborghini", "Huracan", 630, 325)
car3 = SportsCar("McLaren", "720S", 710, 341)
print(car1.display_info()) # ๐ฅ Ferrari F8 Tributo | HP: 710 | Top Speed: 340 km/h ๐
print(car2.display_info()) # ๐ฅ Lamborghini Huracan | HP: 630 | Top Speed: 325 km/h ๐
print(car3.display_info()) # ๐ฅ McLaren 720S | HP: 710 | Top Speed: 341 km/h ๐
๐ฅ Ferrari F8 Tributo | HP: 710 | Top Speed: 340 km/h ๐
๐ฅ Lamborghini Huracan | HP: 630 | Top Speed: 325 km/h ๐
๐ฅ McLaren 720S | HP: 710 | Top Speed: 341 km/h ๐
Key Concept:#
Each object (car) has its own values for
brand
,model
,horsepower
, andtop_speed
, making them unique, they all have the same base attributes and schema defined by the class, but different values.
๐ Objects: Customizing Speed Machines#
Objects allow us to create customized sports cars while still following a standardized blueprint.
Example: Modifying a Carโs Attributes#
# Upgrading the Ferrari
car1.horsepower = 720 # More power!
print(car1.display_info()) # ๐ฅ Ferrari F8 Tributo | HP: 720 | Top Speed: 340 km/h ๐
๐ฅ Ferrari F8 Tributo | HP: 720 | Top Speed: 340 km/h ๐
Note this modified the object car1
and not the class SportsCar
. Thus other cars will not be affected by this change. This is right because my Subaru Forester sure doesnโt have 720 horsepower.
Key Concept:#
Objects can be modified individually, just like tuning a sports car.
๐ Summary#
โ Classes define the blueprint of high-speed beasts.
โ Instance attributes make each sports car unique.
โ Class attributes define shared characteristics of all cars.
โ Objects let us create customized speed machines while following the same elite standards.
๐๏ธ Just like sports cars dominate the roads, OOP helps structure code with speed and efficiency! ๐๐จ