Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Setting and retrieving default value for an attribute in a class

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading