I am in the middle of learning how to create subclasses to a superclass in python. I have created an IceCreamStand subclass to the superclass Restaurant and then gave the subclass a specific attribute. The only problem is when I go to print out the attribute of the subclass, it says that the subclass does not have this attribute.
class Restaurant:
def __init__(self, name, cuisine_type):
"""Initialize attributes to describe restaurant."""
self.name = name
self.cuisine_type = cuisine_type
self.number_served = 23
def describe_restaurant(self):
print(f"The restaurant name is {self.name}.")
print(f"This restaurant is a {self.cuisine_type} restaurant.")
def open_restaurant(self):
print(f"{self.name} is now open.")
def set_number_served(self, new_number_served):
self.number_served = new_number_served
def increment_number_served(self, increment):
self.number_served += increment
class IceCreamStand(Restaurant):
def __init__(self, name, cuisine_type):
super().__init__(name, cuisine_type)
self.flavor = "Vanilla"
def display_flavors(self):
print("The flavor they offer:")
print(self.flavor)
creamStand = IceCreamStand("Jace's Ice Cream", "Ice cream")
creamStand.describe_restaurant()
creamStand.display_flavors()
Output Error of the above code
I was expecting to get the specific subclass attribute after the other superclass attributes were displayed and never got it.
You need to use
__init__()... your original (which I edited) had a typo which used__intit__()