为什么这个AttributeError?

时间:2017-03-24 20:19:02

标签: python-2.7 class attributeerror

我是Python的新手,我试图使用一个漂亮的库(audiotools)来播放mp3播放列表,就像练习一样。

这是播放曲目列表的类(松散地基于THIS,一旦我发现有一个“没有参数的回调函数,播放器在当前曲目结束时调用”{{3} }):

class Trackplay:
    def __init__(self,
                 track_list,
                 audio_output=audiotools.player.open_output('ALSA'),
                 replay_gain=audiotools.player.RG_NO_REPLAYGAIN):

        self.track_index = INDEX - 1
        self.track_list = track_list

        self.player = audiotools.player.Player(
                audio_output,
                replay_gain,
                self.next_track())

    def next_track(self):
        try:
            self.track_index += 1
            current_track = self.track_list[self.track_index]
            print str(current_track)
            audio_file = audiotools.open(current_track)

            self.player.open(audio_file)   # <---------- error
            self.player.play()
        except IndexError:
            print('playing finished')

然后我打电话:

tp = Trackplay(get_track_list(PATH))

其中get_track_list是一个从dir PATH返回mp3列表的方法。

我得到的错误(在标有“错误”评论的行)是:

  

AttributeError:Trackplay实例没有属性'player'

我不明白发生了什么......但是在这里阅读所有AttributeError个问题,必定是愚蠢的......

player在我看来恰好是Trackplay的属性。其他属性track_indextrack_list似乎没问题,因为行print str(current_track)会打印当前曲目。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

请在此处查看此代码?

reduceByKey

在创建您要分配给confidence_score的{​​{1}}时,您需要致电self.player = audiotools.player.Player( audio_output, replay_gain, self.next_track()) 。在Player存在之前,self.player尝试使用self.next_track()

self.next_track

self.player甚至都没有返回任何内容,因此您为什么要将self.player作为参数传递给def next_track(self): try: self.track_index += 1 current_track = self.track_list[self.track_index] print str(current_track) audio_file = audiotools.open(current_track) self.player.open(audio_file) self.player.play() except IndexError: print('playing finished') 而感到困惑。

那应该是回调吗?如果是这样,您应该将next_track传递给self.next_track() ,而无需将其调用

Player