1. 程式人生 > >笨方法學Python——習題40

笨方法學Python——習題40

around family 方法學 列表 init happy 編程 () pocket

之前例子講過關於字典、列表、字符串、元組,按書中所述,其實這些已經足夠寫一些代碼,但Python屬於面向對象的編程語言,本節所講的類,是必須要掌握的,雖然現在有些懵逼

 1 class Song(object):
 2 
 3     def __init__(self, lyrics):
 4         self.lyrics = lyrics
 5 
 6     def sing_me_a_song(self):
 7         for line in self.lyrics:
 8             print line
 9 
10 happy_bday = Song(["
Happy birthday to you", 11 "I don‘t want to get sued", 12 "So I‘ll stop right there"]) 13 14 bulls_on_parade = Song(["They rally around the family", 15 "With pockets full of shells"]) 16 17 happy_bday.sing_me_a_song() 18 19 bulls_on_parade.sing_me_a_song()

笨方法學Python——習題40