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:
JavaScript
x
10
10
1
class Restaurant:
2
"""creating Restaurant class"""
3
def __init__(self, name, cuisine_type):
4
self.name = name
5
self.cuisine_type = cuisine_type
6
self.number_served = 0
7
8
new_restaurant = ('Secret Sky', 'coffee & sandwiches')
9
new_restaurant.number_served
10
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.
JavaScript
1
10
10
1
class Restaurant:
2
"""creating Restaurant class"""
3
def __init__(self, name, cuisine_type):
4
self.name = name
5
self.cuisine_type = cuisine_type
6
self.number_served = 0
7
8
new_restaurant = Restaurant('Secret Sky', 'coffee & sandwiches')
9
print(new_restaurant.number_served)
10