python-Can';t似乎不调用父类';孩子的方法
这是我的密码:python-Can';t似乎不调用父类';孩子的方法,python,python-3.x,inheritance,Python,Python 3.x,Inheritance,这是我的密码: 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 th
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的最后一行给出了一个错误
但是,在我看来,它应该无法在Mp3File
中找到该方法,然后在父类LossyMusicFile
中查找该方法,因为它确实存在
我尝试将该行更改为self.bitrate=(super().parseBitrate(File(location.info.bitrate))/1000
,以便显式使用父类的方法,但我得到了相同的错误。发生什么事了
如果这是以前问过的问题或是一个愚蠢的问题,我深表歉意,但我在搜索时找不到它,事实上我是愚蠢的。所有实例方法都必须将self作为第一个参数。这里发生的事情是,在parseBitrate()
中,您将self
重命名为br
。您需要解析比特率(self,br)
才能接受比特率。您还需要使用其他方法将self
添加到参数列表中,如getFormat()
您的代码使用了ThisVariableNameingStyle
它与Python的官方风格文档相对
音乐文件
不继承对象
只能在“新样式类”中调用从更高类继承的方法。要使类成为“新样式”,必须从对象
继承
此外,获得一个类似PyCharm的IDE,它可以在将来自动警告您这些错误。您的所有实例方法都必须将self
作为第一个参数。请参阅我更新的答案…使用我的水晶球,我可以告诉您正在运行Python 2…@uoɥpʎzɹC我肯定在尝试使用Python 3。我在做什么python 2?这对于python 3应该是不同的?@lucas755无论是python 2还是python 3,您都应该从音乐文件
中的对象继承。这将解决Python2中的问题,但Python3中不会出现此问题,因为在Python3中,所有内容都继承自object,但出于兼容性原因,您仍应继承自object这是Python3.x,因此内置了从object
继承的功能。非常漂亮,谢谢。自从在Java中从事一个项目以来,我已经有一段时间没有学习python了。看来我染上了一些坏习惯@顺便说一句,lucas755 PyCharm是免费的
line 113, in __init__
self.bitrate = (self.parseBitrate(File(location).info.bitrate))/1000
NameError: name 'parseBitrate' is not defined