gpt4 book ai didi

python - 似乎无法从子类调用父类的方法

转载 作者:行者123 更新时间:2023-12-01 03:47:29 25 4
gpt4 key购买 nike

这是我的代码:

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) 才能接受比特率。您还需要将 self 添加到其他方法(如 getFormat())中的参数列表中。

  1. 您的代码使用了 thisVariableNamingStyle,它违反了 Python 的官方样式文档 PEP 8 .
  2. MusicFile 不会继承 object您只能在“新式类”中调用从更高类继承的方法。为了使您的类成为“新式”类,您必须继承object .

此外,获取像 PyCharm 这样的 IDE,它可以在将来自动警告您这些错误。

关于python - 似乎无法从子类调用父类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com