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需要聯想報錯位置相關的程序位置,這點很重要。