Reputation: 89
I am trying to pass in a list of items through a class parameter and call the method display_flavors
to print the list. But I am receiving a list object has no attribute error
.
I have rewritten the code that works but I want to rewrite the code to pass in a list instead of calling the method and passing through the list argument. Is it possible?
# Python script Idea to be implemented which has an error
class IceCreamStand(Restaurant): # Inherits the Parent Class "Restaurant"
"""Attempt to represent an Ice Cream Stand"""
def __init__(self, restaurant_name, cuisine_name, flavors):
"""Initialize the attributes of an Ice Cream Stand"""
super().__init__(restaurant_name, cuisine_name)
self.flavors = flavors
def display_flavors(self):
"""Print all the flavors the Ice Cream Stand offers"""
print("These are flavors offered in this Ice Cream Stand:")
for flavor in self.flavors:
print(flavor.title())
list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream", list_of_flavors)
restaurant1.flavors.display_flavors()
======================================================================== Rewritten code that works
class IceCreamStand(Restaurant): # Inherits Parent Class "Restaurant"
"""Attempt to represent an Ice Cream Stand"""
def __init__(self, restaurant_name, cuisine_name):
"""Initialize the attributes of an Ice Cream Stand"""
super().__init__(restaurant_name, cuisine_name)
def display_flavors(self, flavors):
"""Print all the flavors the Ice Cream Stand offers"""
print("These are flavors offered in this Ice Cream Stand:")
for flavor in flavors:
print(flavor.title())
list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream")
restaurant1.display_flavors(list_of_flavors)
========================================================================
AttributeError: 'list' object has no attribute 'display_flavors'
Upvotes: 1
Views: 14740
Reputation: 89
It now works!! Thanks guys for the clarification was stuck forever with that error
# Corrected Script
class IceCreamStand(Restaurant): # Inherits the attributes from Parent Class "Restaurant"
"""Attempt to represent an Ice Cream Stand"""
def __init__(self, restaurant_name, cuisine_name, flavors):
"""Initialize the attributes of an Ice Cream Stand"""
super().__init__(restaurant_name, cuisine_name)
self.flavors = flavors
def display_flavors(self):
"""Print all the flavors the Ice Cream Stand offers"""
print("These are flavors offered in this Ice Cream Stand:")
for flavor in self.flavors:
print(flavor.title())
list_of_flavors = ["chocolate", "strawberry", "banana"]
restaurant1 = IceCreamStand("Dairy King", "Dairy and Ice Cream", list_of_flavors)
restaurant1.display_flavors() # <<< Here is the correction
Output:
These are flavors offered in this Ice Cream Stand:
Chocolate
Strawberry
Banana
Upvotes: 1
Reputation: 24711
When you use a dot operator to call a function, you're generally calling that function with its first argument being the thing before the dot operator. This is why we include self
when we're writing methods inside a class, like display_flavors
.
When you do
restaurant1.flavors.display_flavors()
then Python tries to call the method display_flavors()
on the object restaurant1.flavors
, with no other arguments. However, restaurant1.flavors
is a list, and as a result doesn't have a method called display_flowers()
. Hence your AttributeError
.
Meanwhile, when you do
restaurant1.display_flavors(list_of_flavors)
you're calling the method display_flavors()
on restaurant1
- which is an IceCreamStand
, and which does have a method called that. Said method is given two arguments: restaurant1
(as self
), and list_of_flavors
.
So, doing restaurant1.display_flavors()
instead of restaurant1.flavors.display_flavors()
in your first example should work.
Upvotes: 2