Python创建实例时提示错误takes no arguments,找不到什么原因?
题目描述
在Jupyter Notebook,定义类、创建实例,运行后提示创建实例的代码takes no arguments,实在找不到原因,麻烦帮小白看一下!
题目来源
《Python编程从入门到实践》习题
相关代码
class Restaurant():
def _init_(self, restaurant_name, cuisine_tpye): self.restaurant_name = restaurant_name
self.cuisine_tpye = cuisine_tpye
my_restaurant = Restaurant("KFC","fast food")
提示错误
TypeError Traceback (most recent call last)
<ipython-input-3-c7d16ceda31f> in <module>
5 self.cuisine_tpye = cuisine_tpye 6
----> 7 my_restaurant = Restaurant("KFC","fast food")
TypeError: Restaurant() takes no arguments
回答:
定义类为啥要加括号?要么括号里面写(object)(python2需要),要么去掉括号:
class Restaurant(object): pass
或者:
class Restaurant: pass
以上是 Python创建实例时提示错误takes no arguments,找不到什么原因? 的全部内容, 来源链接: utcz.com/a/163001.html