2016-08-04 185 views
1

這裏調用父類的方法,是我的代碼:蟒蛇 - 似乎無法從孩子

from mutagen.easyid3 import EasyID3 
from mutagen import File 

class MusicFile: 
    """A class representing a particular music file. 

    Children that are intended to be instantiated must initialize fields for 
    the getters that exist in this class. 
    """ 

    def __init__(self, location): 
     self.location = location 

    def getLocation(): 
     return self.location 

    def getArtist(): 
     return self.artist 

    def getAlbum(): 
     return self.album 

    def getTitle(): 
     return self.title 

############################################################################### 


class LossyMusicFile(MusicFile): 
    """A class representing a lossy music file. 

    Contains all functionality required by only lossy music files. To date, that 
    is processing bitrates into a standard number and returning format with 
    bitrate. 
    """ 
    def __init__(self, location): 
     super().__init__(location) 

    def parseBitrate(br): 
     """Takes a given precise bitrate value and rounds it to the closest 
     standard bitrate. 

     Standard bitrate varies by specific filetype and is to be set by the 
     child. 
     """ 
     prevDiff=999999999 
     for std in self.bitrates: 
      # As we iterate through the ordered list, difference should be 
      # getting smaller and smaller as we tend towards the best rounding 
      # value. When the difference gets bigger, we know the previous one 
      # was the closest. 
      diff = abs(br-std) 
      if diff>prevDiff: 
       return prev 
      prevDiff = diff 
      prev = std 

    def getFormat(): 
     """Return the format as a string. 

     look like the format name (a class variable in the children), followed 
     by a slash, followed by the bitrate in kbps (an instance variable in the 
     children). a 320kbps mp3 would be 'mp3/320'. 
     """ 
     return self.format + '/' + self.bitrate 


############################################################################### 

class Mp3File(LossyMusicFile): 
    """A class representing an mp3 file.""" 

    format = "mp3" 

    # Threw a large value on the end so parseBitrate() can iterate after the end 
    bitrates = (32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 
       128000, 160000, 192000, 224000, 256000, 320000, 999999) 

    def __init__(self, location): 
     super().__init__(location) 

     id3Info = EasyID3(location) 
     self.artist = id3Info['artist'][0] 
     self.album = id3Info['album'][0] 
     self.title = id3Info['title'][0] 
     # Once we set it here, bitrate shall be known in kbps 
     self.bitrate = (self.parseBitrate(File(location).info.bitrate))/1000 

現在,當我嘗試實例化一個Mp3File,它給了我一個錯誤上的最後一行Mp3File.__init__()

line 113, in __init__ 
self.bitrate = (self.parseBitrate(File(location).info.bitrate))/1000 
NameError: name 'parseBitrate' is not defined 

然而,在我看來,它應該沒有找對方法在Mp3File,然後尋找父類,LossyMusicFile,如果它確實存在的方法。

我試着將這一行改爲self.bitrate = (super().parseBitrate(File(location).info.bitrate))/1000,這樣它會明確地使用父類的方法,但是我得到了同樣的錯誤。這是怎麼回事?

如果以前有人問過這個問題,或者是一個愚蠢的問題,但是我找不到它,當我搜索時,我實際上是愚蠢的。

+4

所有的實例方法**都必須具有'self'作爲第一個參數。 – James

+0

查看我的更新回答... –

+0

隨着我的水晶球,我可以打電話給你正在運行的Python 2 .... –

回答

2

所有的實例方法都必須具有self作爲第一個參數。這裏發生的是,在parseBitrate()中,您將self更名爲br。您需要parseBitrate(self, br)才能接受比特率。您還需要在其他方法(如getFormat())中將self添加到參數列表中。

  1. 您的代碼使用thisVariableNamingStyle它違背了Python的官方風格文檔PEP 8
  2. MusicFile不會繼承關閉object您只能在「新風格的類」中調用從更高類繼承的方法。爲了讓你的班級「新式」,你必須繼承object

另外,得到一個像PyCharm這樣的IDE,可以在將來自動提醒你這些錯誤。

+2

這是Python 3.x,因此內置來自'object'的繼承。 – Matthias

+0

美麗,謝謝。自從從事Java項目工作後,我一段時間都沒有找到python。看來我撿到了一些壞習慣! – lucas755

+0

@ lucas755 PyCharm是免費的,順便說一句 –