Python : when I call a method of a class it requires me to enter a positional self argument

Question:

I have learned OOP in python recently and I am facing a problem
when I call a method of a class it requires me to enter a positional self argument

class Restaurant:
    menu_items = ["tea", "coffee", "milk"]
    book_table = {"1": "not booked", "2": "not booked", "3": "not booked"}
    customers_order = {}

    def add_menu_item(self):
        item = input("Enter an item to add to menu: ")
        self.menu_items.append(item)

    def print_table_reservation(self):
        print("table reservation: ", self.book_table)

    def all_reserved(self):
        no_table_left = True
        for i  in self.book_table:
            if self.book_table[i] == "not booked":
                no_table_left = False
        return no_table_left

    def book_a_table(self):
        name = input("What's your name: ")
        self.print_table_reservation()
        if self.all_reserved():
            print("Sorry all tables are reserved")
        else:
            while True:
                num = input("what is the number of table[1, 2, 3]: ")
                if self.book_table[num] == "not booked":
                    self.book_table[num] = name
                    break
                else:
                    print("the table is booked plz select another one")
                    continue


    def print_menu(self):
        print("the menu: ", self.menu_items)

    def take_order(self):
        name = input("What's your name: ")
        list1 = []
        if name in self.book_table:
            while True:
                answer = input("Do you want to order")
                if answer == "yes" or "Yes":
                  while True:
                      self.print_menu()
                      order = input("Select an item from the menu")
                      if order in self.menu_items:
                          list1.append(order)
                          break
                      else:
                          print("Item not in menu, please reselect")
                          continue
                else:
                    break
        self.customers_order[name] = list1

    def print_customer_order(self):
        name = input("What's your name")
        if name in self.customers_order:
            print("Your order : ", self.customers_order[name])
        else:
            print("You have not given an order")


coffee = Restaurant
coffee.book_a_table()

when i call book a table function
it says error missing one "self" poitional argument
if I fill it with coffee I stiil cant fill it in the functions inside
why is it requiring an argument?

Asked By: Moha Med

||

Answers:

You are not creating an instance of the Restaurant class in your code, do it with parenthesis like that coffee = Restaurant()

Now coffee.book_a_table() should work

Answered By: Saxtheowl

First, create an instance of your class, so that self is automatically bound when calling methods.

coffee = Restaurant()

To allow for each restaurant (instance) to have separate information, you should set the fields inside __init__ rather than have all the variables attached to the class itself.

def __init__(self):
    self.menu_items = ["tea", "coffee", "milk"]
    self.book_table = {"1": "not booked", "2": "not booked", "3": "not booked"}
    self.customers_order = {}
Answered By: Unmitigated
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.