You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

《笨方法学Python》练习40报错:TypeError: object() takes no parameter

错误原因

  • 构造方法拼写错误:Python 类的构造方法固定为前后各两个下划线的__init__,你写的_init_仅前后各1个下划线,不会被识别为构造方法,程序会默认调用父类object的无参构造方法,传入参数就会触发你遇到的报错。
  • 传参不匹配:你的构造方法仅接收1个lyrics参数,实例化Song时你传入了3个独立的字符串,需要把歌词打包为列表作为单个参数传入。

修正后可运行代码

class Song(object):
    def __init__(self, lyrics):
        self.lyrics = lyrics
    def sing_song(self):
        for line in self.lyrics:
            print line

happy_bday = Song(["Happy Birthday to you",
            "I might get sued for this",
            "So I'll stop right here"])
            
print happy_bday

happy_bday.sing_song()

内容的提问来源于stack exchange,提问作者Binod Bista

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 16:06:02