以艰难的方式学习 Python ex40 : TypeError :object () 不带参数

时间:2021-07-18 03:04:56

标签: python class object

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()

***这给了我错误***

Traceback (most recent call last):
  File "ex36.py", line 9, in <module>
    "So I'll stop right here")
TypeError: object() takes no parameters

我在这里遗漏了什么吗?提前致谢

1 个答案:

答案 0 :(得分:2)

你需要使用

def _init_(self, lyrics):

代替

__init__

在你的课堂上。此外,您的 def __init__(self, *lyrics): 函数仅接受 1 个参数,而您传入​​ 3 个参数。如果您希望它将所有参数作为列表,您可以使用

lyrics

function ScrollToBottom(){ const elementRef = useRef(); useEffect(() => elementRef.current.scrollIntoView()); return <div ref={elementRef} />; }; 将是您传入的所有参数的列表。