Search code examples
pythonclassattributeerror

AttributeError: 'Restaurant' object has no attribute 'flavours' - why?


## class restaurant ##

Implemented superclass

class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
    self.restaurant_name = restaurant_name
    self.cuisine_type = cuisine_type 
def describe_restaurant(self):
    print('This restaurant is called ' + self.restaurant_name + '.')
    print('This restaurant serves dishes acoording to ' + self.cuisine_type + '.')
def open_restaurant(self, hours):
    self.hours = hours
    print(self.restaurant_name.title() + ' is opened ' + str(hours) + '!')

class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type):
    super().__init__(restaurant_name, cuisine_type)
    self.restaurant_name = restaurant_name
    self.cuisine_type = cuisine_type
    flavours = ['chocolate', 'vanilia', 'strawberry', 'lime', 'orange']
def flavours(self):
    print('Available flavours: ')
    for flavour in flavours:
        print(flavour)
IceCreamStand  = Restaurant(' Matt IceCream ', 'ice creams')
IceCreamStand.describe_restaurant()
IceCreamStand.flavours()

Solution

  • Because Restaurant, indeed, does not have an attribute flavours; IceCreamStand does, or at least did, until you replaced that class with an instance of Restaurant with IceCreamStand = Restaurant(...).

    Use a different variable name (camel-case for class names, snake-case with initial lowercase for objects), and create an instance of IceCreamStand.

    ice_cream_stand = IceCreamStand(' Matt IceCream ', 'ice creams')