学习python出现的问题_类初始化

代码如下:

class song(object):
    def __int__(self,lyrics):
        self.lyrics = lyrics
    def singsong(self):
        for line in self.lyrics:
            print line

happy_bday = song(["happy birthday to you",
    "I don't want to get sued",
    "so i'll stop singing."])
bulls = song(["They rally around the family",
    "With pockets full of shells"])

happy_bday.singsong()
bulls.singsong()

刚学习python,对于其中的类初始化好不熟练。代码尝试创建一个song类,后创建两个对象happy_bday和bulls,使用初始化的时候提示报错:

Traceback (most recent call last):
  File "class.py", line 10, in <module>
    "so i'll stop singing."])
TypeError: object() takes no parameters

应该是对象happ_bday的初始化发生了问题,查了overstack说是_int_ 函数在python中的初始化要加两个下划线__int__,修改后无果。

于是我考虑将对象创建的过程用以下代码重写:

happy_bday = song()
happy_bday.lyrics = ["happy birthday to you",
    "I don't want to get sued",
    "so i'll stop singing."]

bulls = song()
bulls.lyrics = ["They rally around the family",
    "With pockets full of shells"]
happy_bday.singsong()
bulls.singsong()

显示没有问题,那就应该还是初始化函数__int__的问题。
定睛一看,应该是__init__才对。
故事告诉我们:要好好休息放松眼睛。

你可能感兴趣的:(python,python,编辑器,class,parameter)