Python Forum

Full Version: Giving class multiple arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to create a simple class that accepts 2 args.

It routinely responds:
TypeError: Restaurant() takes 1 positional argument but 2 were given

EXAMPLE:
def Restaurant(object): <--using the 'object' parameter seems to only allow 1 parameter
"""show info about restaurant""" according to my textbook(using python 3) this should be enough
def __init__(self, name, food):
#define attributes
self.name = name
self.food = food
def restaurant_info(self):
print(self.name.title() + " serves " +
self.food + "food.")
def restaurant_open(self):
print(self.name.title() + " is now open!")

my_restaurant = Restaurant('caputos', 'italian')
You must use class key word instead of def for Restaurant class:

class Restaurant(object):

    def __init__(self, name, food):
        self.name = name
        self.food = food

    def restaurant_info(self):
        print(self.name.title() + " serves " + self.food + "food.")

    def restaurant_open(self):
        print(self.name.title() + " is now open!")


my_restaurant = Restaurant('caputos', 'italian')