TypeError: Restaurant() takes no arguments

 

1. 错误描述

TypeError: Restaurant() takes no arguments

TypeError: Restaurant() takes no arguments

 

2. 原因:在编写__init__时,pycharm会自动添加关键字,有时会直接写称整型int, 即__int__。导致错误产生。

 

————————————————参考————————————————————————————————————————————————————————

3. 错误代码

# 9-1 restaurant
class Restaurant():
    def __int__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print("The " + self.restaurant_name + " have " +
              str(self.cuisine_type) + " kinds of food.")

    def open_restaurant(self):
        print("Now is opening.")

restaurant = Restaurant("'Restaurant of peace'", 108)
restaurant.describe_restaurant()
restaurant.open_restaurant()

  

4. 正确代码

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print("The " + self.restaurant_name + " have " +
              str(self.cuisine_type) + " kinds of food.")

    def open_restaurant(self):
        print("Now is opening.")

restaurant = Restaurant("'Restaurant of peace'", 108)
restaurant.describe_restaurant()
restaurant.open_restaurant()

  

  

5.  执行结果

TypeError: Restaurant() takes no arguments

 

上一篇:requests+unittest接口自动化之报错:TypeError: list indices must be integers or slices, not str


下一篇:成功解决TypeError: tuple indices must be integers or slices, not str