Python Forum
Problems with inheritance with classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with inheritance with classes
#1
I'm getting a type error.
I want to write a subclass and add an attribute to it, then write a method to display the attribute and call the method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Class Restaurant ():
 
def __init__(self, restaurant_name, cuisine_name):
self.restaurant_name = restaurant_name
self.cuisine_name = cuisine_name
 
def describe_restaurant (self):
print("This is " + self.restaurant name.title())
print(self.cuisine_name.title() + " is the only cuisine right now")
 
def open_restaurant(self):
print(self.restaurant_name.title() + " is open for business ")
 
class IceCreamStand(Restaurant):
 
def __init__(self, restaurant_name, cuisine_name):
super().__init__(restaurant_name, cuisine_name)
self.flavors = flavors
 
def print_flavors(self):
print(self.flavors)
 
IceCream = IceCreamStand('dominos', 'pizza', 'BBQ')
 
IceCream.IceCreamStand.print_flavors()
So basically, I have a class Restaurant and I have a subclass IceCreamStand and I added an attribute flavors to the subclass and a method print_flavors to display the flavors, then I have to call the method.

The specific error I get is TypeError: __init__ takes 3 positional arguments but 4 were given.
Any help would be appreciated.
Reply
#2
2 parameters defined, 3 arguments given
1
2
3
4
5
6
7
class IceCreamStand(Restaurant):
  
def __init__(self, restaurant_name, cuisine_name):
super().__init__(restaurant_name, cuisine_name)
self.flavors = flavors
 
IceCream = IceCreamStand('dominos', 'pizza', 'BBQ')
Reply
#3
Hey. I have a similar program and it works perfectly for me. Hope it helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Restaurant():
    """Information storage for restaurants"""
    def __init__(self,name,cuisine):
        self.name=name
        self.cuisine=cuisine
        self.served=0
    def describe_rest(self):
        first=self.cuisine[0:1]
        if first.lower()=='a' or first.lower()=='i' or first.lower() =='o' or first.lower()=='u' or first.lower()=='e':
            print(self.name.title() + " " + "is an " + self.cuisine.title()+ " " + "restaurant")
        else:
            print(self.name.title() + " " + "is a " + self.cuisine.title()+ " " + "restaurant")
    def rest_open(self):
        print(self.name.title()+ " " +  "is open now")
    def number_served(self):
        """Prints the number of customers serves by the restaurant"""
        number=str(self.served) + " " + "customers have been served"
        return number
    def set_number_served(self,people):
        """Allows to set the number of people served"""
        self.people=people
        print(str(self.people)+ " " + "people have been served")
    def increment_served_people(self,people_served):
        """Adjusts the number of people served by providing option to increment"""
        print("Number of customers served is " + " " + str(self.people+people_served))
class IceCream_Stand(Restaurant):
    def __init__(self,name,cuisine):
        super().__init__(name,cuisine)
    def flavours(self):
        self.flavours=[]
        self.flavours.append('chocolate')
        self.flavours.append('strawberry')
        self.flavours.append('rocky road')
        self.flavours.append('cotton candy')
        print("Flavours availale today are:")
        for flavour in self.flavours:
            print("\n\t" + flavour.title())
my_icecream=IceCream_Stand('pinkertons' , 'desserts')
my_icecream.describe_rest()
my_icecream.flavours()
Reply
#4
This drives me nuts to see in python
Quote:
1
print(self.name.title() + " " + "is an " + self.cuisine.title()+ " " + "restaurant")

As it should be
1
print(f'{self.name.title()} is an {self.cuisine.title()} restaurant')
or at the very worst if they dont have python3.6+
1
print('{} is an {} restaurant'.format(self.name.title(), self.cuisine.title()))
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python inner classes inheritance from parent class Abedin 8 1,104 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Inheritance vs Instantiation for Python classes mr_byte31 7 5,663 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Unexpected Output using classes and inheritance langley 2 2,620 Jul-04-2019, 09:33 AM
Last Post: langley
  Problems with importing classes in different folder Xeraphim 3 4,252 Nov-08-2017, 03:20 PM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 6,515 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020