๐ŸŽ๏ธ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, and top_speed, making them unique, they all have the same base attributes and schema defined by the class, but different values.

๐Ÿš˜ Class Attributes: Shared Characteristics of All Cars#

Class attributes are shared across all objects. They define properties common to every instance. These are defined outside the __init__ method, and thus are not modified during instantiation.

Example: Class Attribute in Action#

print(car1.category)  # Sports Car
print(car2.category)  # Sports Car
Sports Car
Sports Car

Modifying a Class Attribute#

Throughout this class we have called sub-methods and used classes. When you import sub-modules from packages, you are using class attributes. For example, when you import the math module, you are using the class attribute pi. This can be called with math.pi. The . operator is used to access class attributes.

You can also modify class attributes by using the . operator. For example, you can change all the carsโ€™ wheels attribute to 4 by using SportsCar.wheels = 4.

# Changing the category for ALL sports cars
SportsCar.category = "High-Performance Monster"
SportsCar.wheels = 4

print(car1.category)  # High-Performance Monster
print(car2.category)  # High-Performance Monster
print(car2.wheels) 
print(car1.wheels)
High-Performance Monster
High-Performance Monster
4
4

Key Concept:#

  • Class attributes are shared across all instances of the class.

  • Changing the class attribute modifies it for every object.

๐Ÿ 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! ๐Ÿš€๐Ÿ’จ