Setting and retrieving default value for an attribute in a class

Question:

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’

Asked By: Khalid Riaz

||

Answers:

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)
Answered By: Luke
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.