Python Forum
Giving class multiple arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Giving class multiple arguments
#1
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')
Reply
#2
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class takes no arguments bily071 2 633 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  Can I use logging in a class (without multiple messages) mevan 2 590 Oct-16-2023, 11:08 PM
Last Post: mevan
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,317 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Class Takes No Arguments horuscope42 4 4,817 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  How to pass multiple arguments into function Mekala 4 2,447 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Class takes no arguments Nazartfya 2 4,569 Jun-27-2020, 12:45 PM
Last Post: Nazartfya
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 4,603 Feb-12-2020, 04:11 PM
Last Post: psolar
  Class takes no arguments Myang123 3 9,792 Nov-26-2019, 12:01 PM
Last Post: Davy_Jones_XIV
  Using function *args to multiply multiple arguments allusernametaken 8 6,064 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Paste Special with multiple arguments with Dispatch CaptainCsaba 3 5,692 Jun-20-2019, 07:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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