鱼C论坛

 找回密码
 立即注册
查看: 1567|回复: 2

请问这一部分代码哪里出现问题了呢?Jupyter给的解释不太理解

[复制链接]
发表于 2022-3-19 14:57:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class Restaurant:
    """一次模拟餐厅的简单尝试"""
   
    def _init_(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        
    def describe_restaurant(self):
        print(f"The restaurant name is {self.restaurant_name}.")
        print(f"The cuisine type is {self.cuisine_type}.")
        
    def open_restaurant(self):
        print("This restaurant is opening.")
        
restaurant_1 = Restaurant('a','b')

restaurant_1.describe_restaurant()
restaurant_1.open_restaurant()


TypeError                                 Traceback (most recent call last)
<ipython-input-15-6e85ab6df72d> in <module>()
     14         print("This restaurant is opening.")
     15
---> 16 restaurant_1 = Restaurant('self','a','b')
     17
     18 restaurant_1.describe_restaurant()

TypeError: Restaurant() takes no arguments
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-19 15:00:47 | 显示全部楼层
是 __init__ 不是 _init_ 。
  1. class Restaurant:
  2.     """一次模拟餐厅的简单尝试"""

  3.     def __init__(self, restaurant_name, cuisine_type): # 注意,__init__旁边是两个下划线
  4.         self.restaurant_name = restaurant_name
  5.         self.cuisine_type = cuisine_type

  6.     def describe_restaurant(self):
  7.         print(f"The restaurant name is {self.restaurant_name}.")
  8.         print(f"The cuisine type is {self.cuisine_type}.")

  9.     def open_restaurant(self):
  10.         print("This restaurant is opening.")


  11. restaurant_1 = Restaurant("a", "b")

  12. restaurant_1.describe_restaurant()
  13. restaurant_1.open_restaurant()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-19 15:04:09 | 显示全部楼层
是__init__,每边两个下划线,不是一个下划线
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 13:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表