1、学习Python类的部分第一天遇到报错TypeError: Restaurant() takes no arguments
过年啦,因为疫情也不让出门,拿了本python的书开始自学
刚学习类就遇到如题的报错,研究半天发现是初始化类的函数def init(self,restaurant_name,cuisine_type):中下划线init前两条后面也是两条,我写成了各一条,下面是我按照《Python编程从入门到实践》一书类部分一个任务编写的程序
class Restaurant():
def _init_(self,restaurant_name,cuisine_type):
self.restaurant_name=restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print(self.restaurant_name.title()+
"'s cuisine type is "+
self.cuisine_type+'.')
def open_restaurant(self):
print('The restaurant named '+
self.restaurant_name.title()+
'is open.')
my_restaurant=Restaurant('xiaoweifandian','breakfast')#报错提示这行有问题
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
报错的终端窗口如下:
修改后程序正常运行,这时的终端窗口显示如下:
有一点要注意,虽然报错是line 13,但实际上我写错的地方时不是这里,所以找bug需要联想报错位置相关的程序位置,这点很重要。