2017-09-30 122 views
-1

我似乎無法弄清楚是什麼導致了下面的屬性錯誤。我真的不確定我在這裏做錯了什麼。任何幫助將是偉大的!Python的屬性錯誤

回溯(最近通話最後一個): 文件 「project2.py」,線路140,在 movie_writter.writerow([movie.title,movie.author,movie.itunes_id,movie.itunes_URL,movie.length] ) AttributeError的:「電影」對象有沒有屬性「長度」

class Movie(Media): 
    def __init__(self, diction_file): 
     Media.__init__(self, diction_file) 
     self.rating = diction_file['contentAdvisoryRating'] 
     self.genre = diction_file['primaryGenreName'] 
     try: 
      self.track_time = diction_file['trackTimeMillis'] 
     except: 
      self.track_time = 0 
     try: 
      self.description = diction_file['longDescription'] 
     except: 
      self.description = None 

    def __len__(self): 
     return self.track_time/1000 
    def title_words_num(self): 
     if self.description != None: 
      return len(self.description.split()) 
     else: 
      return 0 

    movie_list = [Movie(diction_file) for diction_file in movie_samples] 

    with open('movies.csv', 'w', newline = '') as movies_csv: 
     movie_writter = csv.writer(movies_csv, delimiter=',') 
     movie_writter.writerow(["Title", "Artist", "ID", "URL", "Length"]) 
     for movie in movie_list: 
      movie_writter.writerow([movie.title, movie.author, movie.itunes_id, movie.itunes_URL, movie.length]) 
    movies_csv.close() 
+2

我們可以看到你的電影對象嗎? –

+0

@PatrickHaugh我剛編輯我的帖子以顯示電影對象,謝謝! – Avery9115

+0

@roganjosh我添加了我創建的Movie類,謝謝! – Avery9115

回答

3

這意味着這意味着什麼:對象有沒有屬性length。你大概的意思是len(movie),它映射到__len__魔法。

此外,請記住,這不是__len__方法的最佳使用方法。它應該用於任何種類的收藏品/容器,以展示其中的一些物品。電影的持續時間不是一定數量的事情。

+0

非常感謝! – Avery9115