# page 53 ### Product class definition ### class Product: # best practice class names are Title Case # define the attributes of this class in the __init__() def __init__(self, name, price, description): # self.instance name = "passed" parameter self.name = name # use self.name in our other objects self.price = price self.description = description # define an instance method that shows product info def display_product_info(self): print(f"Product: {self.name}") print(f"Price: ${self.price}") print(f"Description: {self.description}") # define a string method in the class page 56 # preferred f"" but parameterize or join + def __str__(self): # __str__ access to the instance vars return f"Product: {self.name} Price: ${self.price}" ### end of the Product class definition ### ### DiscountedProduct class definition ### class DiscountedProduct(Product): # injecting the Product class # define __init__() parent params first then new params def __init__(self, name, price, description, discount): super().__init__(name, price, description) self.discount = discount # new method in the subclass def calc_disc_price(self): disc_price = self.price - ( self.price * self.discount / 100 ) # 10% not .10 return disc_price # page 62 Override Pass all 4 args to function # Overload Pass the overloaded args to function def display_product_info(self): super().display_product_info() # 3 print() line from parent print(f"Discount Percentage: {self.discount}%") print(f"Discounted Price: ${self.calc_disc_price()}") ### EO DiscountedProduct class definition ### # Create an instance of the Product class object product1 = Product("Blue Running Shoe", 52.50, "Men's Blue Running Shoe") # Create an instance of the Discounted Product class object discounted_product1 = DiscountedProduct("Blue Running Shoe", 52.50, "Men's Blue Running Shoe", 25) product2 = Product("Red Running Shoe", 62.50, "Womens's Red Running Shoe") # Access value of an attribute print("---- Product 1 Information ----") print("Product Name:", product1.name , "Product Price:", product1.price) product1.display_product_info() # 3 lines of return print("---- Discounted Product 1 Information ----") discounted_product1.display_product_info() # 3 lines parent 2 lines subclass print("---- End of Product 1 Information ----\n\n") # for keyword to loop thru BOTH product objects products =[ Product("Blue Running Shoe", 52.50, "Men's Blue Running Shoe"), Product("Red Running Shoe", 62.50, "Womens's Red Running Shoe") ] for product in products: # for x in ys print(product.display_product_info()) print("---------") # use the __str__ method to output product1 print("---Product 1 Information using the __str__ ---") print(product1) print("---------------")