2

I'm having a little trouble understanding these two particular concepts in Python. I feel like I get the gist of them, but don't understand them completely.

From what I have gathered, super() calls the init method from the parent class and handles the arguments you pass to it from the parent class.

Why then would there need to be an init method in the subclass that includes the same parameters as those in the superclass? Is it because both classes are still similar in nature and the subclass would therefore require some of the basic features of the superclass? I know that most classes require a constructor, just wondering why you would need to re-define the same parameters in a subclass constructor like those in the superclass, and have a super() function within the body of that constructor.

An ex of code, would be something like this:

#9-6 Ice Cream Stand

class Restaurant:
    
    def __init__(self, restaurant_name, cuisine_type):
        """Set attributes of a restaurant"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        """Print a statement about the restaurant"""
        print(f"Welcome to {self.restaurant_name.title()}! Enjoy our amazing {self.cuisine_type.title()} cuisine!")

    def open_restaurant(self):
        """Print a message indicating the restaurant is open"""
        print(f"{self.restaurant_name.title()} is open and ready to serve!")

    def set_number_served(self, total_served):
        self.number_served = total_served

    def increment_number_served(self, additional_served):
        self.number_served += additional_served


class IceCreamStand(Restaurant):

    def __init__(self, restaurant_name, cuisine_type='ice cream'):#Same params as in superclass contructor
        """Initialize the attributes of the parent class
        Then initialize the attributes specific to an ice cream stand."""
        super().__init__(restaurant_name, cuisine_type)
        self.flavors = []

    def display_flavors(self):
        """Display the ice cream flavors"""      
        for flavor in self.flavors:
            print(f"- "  + f"{flavor}")

ic_stand1 = IceCreamStand('tasty ice cream')
ic_stand1.flavors = ['chocolate', 'vanilla', 'strawberry']
ic_stand1.display_flavors()

ic_stand1.describe_restaurant()

Can anyone kindly walk this through for me, so I can get a better picture and grasp of these concepts? All help is appreciated.

Alexander
  • 59,041
  • 12
  • 98
  • 151

1 Answers1

2

The process of constructing an IceCreamStand involves the process of constructing a Resturant, plus some other stuff (e.g. initialize the flavours). To construct a Resturant, you need the restaurant_name and the cuisine_type.

But the __init__ of IceCreamStand doesn't know what name you want. So just like how a Resturant asks you to provide the restaurant_name, so does the IceCreamStand.

The one odd-ball here is that the __init__ of IceCreamStand has a cuisine_type defaulted to 'ice cream'. This doesn't make sense, because it would allow a caller to say:

strange_icecream_stand = IceCreamStand("Strange icecream stand", cuisine_type = 'meat balls') # Meat balls ?!

Instead, it would make sense to define it like so:

class IceCreamStand(Restaurant):

    def __init__(self, restaurant_name)
        """Initialize the attributes of the parent class
        Then initialize the attributes specific to an ice cream stand."""
        super().__init__(restaurant_name, 'ice cream') # The `cuisine_type` is always ice cream now
        self.flavors = []

    # ...

Alexander
  • 59,041
  • 12
  • 98
  • 151