http://learnpythonthehardway.org/book/ex40.html
将歌词放在单独的变量中,然后将该变量传递给类以供使用。
我不知道怎么做,我甚至不知道他说的是什么意思。他的意思是定义类中的变量吗?
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 birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
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()发布于 2013-09-10 06:20:55
我只是觉得他想做些像
lyricsImagine = ["Imagine there's no countries","It isn't hard to do","Nothing to kill or die for"]
songImagine = Song(lyricsImagine)
songImagine.sing_me_a_song()发布于 2013-09-14 16:07:07
我认为他的意思是你应该这样做:
foo = Song(happy_bday.lyrics)
foo.sing_me_a_song()重要的一点是.lyrics,如果不包括它,那么它将给您一个TypeError
发布于 2017-07-08 02:07:48
class Song(object):
def __init__(self):
self.lyrics =(["Happy birthday to you",
"I don' want to get sued",
"So I'll stop right there",
"They rally around the family",
"With pockets full of shells"])
def sing_me_a_song(self):
for line in self.lyrics:
print line
happy_bday = Song()
happy_bday.sing_me_a_song()https://stackoverflow.com/questions/18711043
复制相似问题