2017-04-14 48 views
0

我想定義一個類,它返回帶有歌曲標題,曲目號和流派類型的字典(這是來自特定藝術家對歌曲的iTunes API請求)。目前,當我在終端中運行我的文件時,沒有任何內容正在返回(我沒有收到語法錯誤或回溯)。我期待看到一個帶有曲目名稱,曲目編號和另類風格的詞典。任何幫助將是偉大的!緩存JSON的類定義

class Song(): 
def __init__(self, song_dict={}): 
    self.trackName = song_dict["trackName"] 
    self.trackNumber = song_dict["trackNumber"] 
    self.genre = song_dict["primaryGenreName"]["alternative"] 
def __str__(self): 

return "{} by {}".format(self.trackName,self.trackNumber,self.genre) 

緩存JSON:

{"http://itunes.apple.com/searchcountry-US_entity-song_format-json_term-bon+iver": {"resultCount": 50, "results": [{"collectionExplicitness": "notExplicit", "releaseDate": "2011-06-21T07:00:00Z", "currency": "USD", "artistId": 273428126, "previewUrl": "http://audio.itunes.apple.com/apple-assets-us-std-000001/AudioPreview71/v4/29/b7/6d/29b76dbc-c7c2-a528-98c0-88250c90d61f/mzaf_8791054078902679680.plus.aac.p.m4a", "trackPrice": 1.29, "isStreamable": true, "trackViewUrl": "https://itunes.apple.com/us/album/holocene/id438685974?i=438685989&uo=4", "collectionName": "Bon Iver", "collectionId": 438685974, "trackId": 438685989, "collectionViewUrl": "https://itunes.apple.com/us/album/holocene/id438685974?i=438685989&uo=4", "trackCount": 10, "trackNumber": 3, "discNumber": 1, "collectionPrice": 9.99, "trackCensoredName": "Holocene", "trackName": "Holocene", "trackTimeMillis": 336613, "primaryGenreName": "Alternative", "artistViewUrl": "https://itunes.apple.com/us/artist/bon-iver/id273428126?uo=4", "kind": "song", "country": "USA", "wrapperType": "track", "artworkUrl100": "http://is3.mzstatic.com/image/thumb/Music/v4/f6/41/9b/f6419bd5-7fa7-6046-04d0-fcae6ecf9d96/source/100x100bb.jpg", "collectionCensoredName": "Bon Iver", "artistName": "Bon Iver", "artworkUrl60": "http://is3.mzstatic.com/image/thumb/Music/v4/f6/41/9b/f6419bd5-7fa7-6046-04d0-fcae6ecf9d96/source/60x60bb.jpg", "trackExplicitness": "notExplicit", "artworkUrl30": "http://is3.mzstatic.com/image/thumb/Music/v4/f6/41/9b/f6419bd5-7fa7-6046-04d0-fcae6ecf9d96/source/30x30bb.jpg", "discCount": 1}, {"contentAdvisoryRating": "Explicit", "collectionExplicitness": "explicit", "releaseDate": "2010-11-22T08:00:00Z", "currency": "USD", "artistId": 2715720, "previewUrl": "http://a1044.phobos.apple.com/us/r30/Music/ac/5a/7b/mzm.wurlfubj.aac.p.m4a", "trackPrice": 1.29, "isStreamable": true, "trackViewUrl": "https://itunes.apple.com/us/album/monster-feat-jay-z-rick-ross-nicki-minaj-bon-iver/id403822142?i=403822318&uo=4", "collectionName": "My Beautiful Dark Twisted Fantasy", "collectionId": 403822142, "trackId": 403822318, "collectionViewUrl": 
+1

你還沒有說過你遇到了什麼問題。這個代碼至少有四件事是錯誤的:縮進,從init方法返回的東西,在song_dict和它的鍵之間的不必要的等號,以及可變的默認參數。 –

回答

0

兩件事情:

  1. 首先,你有一些等號(意思是,分配),你並不需要/想。這:

    self.trackName =song_dict["trackName"] 
    self.trackNumber=song_dict=["trackNumber"] 
    self.primaryGenreName =song_dict=["primaryGenreName"] 
    

    應該成爲這樣的:

    self.trackName = song_dict["trackName"] 
    self.trackNumber = song_dict["trackNumber"] 
    self.primaryGenreName = song_dict["primaryGenreName"] 
    
  2. 接下來,你不從在Python類構造函數(__init__)返回的東西。所以刪除return語句。

+0

感謝您的提示。我刪除了等號和返回語句。再建議?謝謝! – sammie1991