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

阅读 12.3k
1 个回答

定义类为啥要加括号?要么括号里面写(object)(python2需要),要么去掉括号:

class Restaurant(object):
    pass

或者:

class Restaurant:
    pass
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题