2016-08-04 9 views
1

から親クラスのメソッドを呼び出すように見えることはできませんが、私のコードです:のpython - ここでは、子

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は無料です、btw –