这是我的代码:
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
,以便明确使用父类的方法,但是我得到了相同的错误。发生了什么事?
道歉,如果以前曾被问过这个问题,或者是一个愚蠢的问题,但是当我搜索时我找不到它,事实上,我是愚蠢的。
答案 0 :(得分:2)
所有实例方法都必须将self作为第一个参数。这里发生的是parseBitrate()
您将self
重命名为br
。您需要parseBitrate(self, br)
才能接受比特率。您还需要在self
等其他方法中将getFormat()
添加到参数列表中。
thisVariableNamingStyle
来对付Python的官方风格文档PEP 8。MusicFile
不会继承object
。 您只能在"新式课程中调用从更高级别继承的方法"。为了使您的课程成为新式",您必须继承离开object
。此外,获取像PyCharm这样的IDE,可以在将来自动警告您这些错误。