class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line) happy_bday = Song(["happy birthdar yo you"]) bulls_on_parade = Song(["they rally around the family", "with pockets full of shells"]) happy_bday.sing_me_a_song() bulls_on_parade.sing_me_a_song()
输出结果:
注意:
__init__ 的左右两侧各有两个“_”,如果只输入一个,会出现:TypeError: Song() takes no arguments