Skip to content
Advertisement

Setting and retrieving default value for an attribute in a class [closed]

I am using python in a google colab notebook. I created a Restaurant class and set the default value for an attribute number_served = 0. Also created an instance, new_restaurant, of the class. I get an error when I try to retrieve the attribute’s value for the instance:

class Restaurant:
  """creating Restaurant class"""
  def __init__(self, name, cuisine_type):
    self.name = name
    self.cuisine_type = cuisine_type
    self.number_served = 0
  
new_restaurant = ('Secret Sky', 'coffee & sandwiches')
new_restaurant.number_served

Error message:

AttributeError Traceback (most recent call last) in () 7 8 new_restaurant = (‘Secret Sky’, ‘coffee & sandwiches’) —-> 9 new_restaurant.number_served

AttributeError: ‘tuple’ object has no attribute ‘number_served’

Advertisement

Answer

When you create an instance of your Restaurant Class, you have to call it like so.

class Restaurant:
  """creating Restaurant class"""
  def __init__(self, name, cuisine_type):
    self.name = name
    self.cuisine_type = cuisine_type
    self.number_served = 0
  
new_restaurant = Restaurant('Secret Sky', 'coffee & sandwiches')
print(new_restaurant.number_served)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement