蟒蛇 - 似乎无法从孩子
问题描述:
这里调用父类的方法,是我的代码:蟒蛇 - 似乎无法从孩子
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
,这样它会明确地使用父类的方法,但是我得到了同样的错误。这是怎么回事?
如果以前有人问过这个问题,或者是一个愚蠢的问题,但是我找不到它,当我搜索时,我实际上是愚蠢的。
答
所有的实例方法都必须具有self作为第一个参数。这里发生的是,在parseBitrate()
中,您将self
更名为br
。您需要parseBitrate(self, br)
才能接受比特率。您还需要在其他方法(如getFormat()
)中将self
添加到参数列表中。
- 您的代码使用
thisVariableNamingStyle
它违背了Python的官方风格文档PEP 8。 -
MusicFile
不会继承关闭object
。 您只能在“新风格的类”中调用从更高类继承的方法。为了让你的班级“新式”,你必须继承object
。
另外,得到一个像PyCharm这样的IDE,可以在将来自动提醒你这些错误。
所有的实例方法**都必须具有'self'作为第一个参数。 – James
查看我的更新回答... –
随着我的水晶球,我可以打电话给你正在运行的Python 2 .... –