Python 在readline中完成函数是如何工作的

Python 在readline中完成函数是如何工作的,python,autocomplete,readline,Python,Autocomplete,Readline,我查了几个例子,包括 我不明白为什么状态是递增的,它的目的是什么。 当state=0且匹配了一个元素列表中的单词时,它可以正常工作,但当state增加时,我将引发一个错误(IndexError) 感谢状态是可能的匹配数,您可以使用它返回多个结果。 i、 e.当state==1返回“cmd1”,当state==2返回“command2”,当state==3返回None时,通过2个匹配结果 class SimpleCompleter(object): def __init__(self,

我查了几个例子,包括

我不明白为什么状态是递增的,它的目的是什么。 当state=0且匹配了一个元素列表中的单词时,它可以正常工作,但当state增加时,我将引发一个错误(IndexError)

感谢

状态是可能的匹配数,您可以使用它返回多个结果。 i、 e.当
state==1
返回“cmd1”,当
state==2
返回“command2”,当
state==3
返回None时,通过2个匹配结果

class SimpleCompleter(object):

    def __init__(self, options):
        self.options = sorted(options)
        return

    def complete(self, text, state):
        response = None
        if state == 0:
            # This is the first time for this text, so build a match list.
            if text:
                self.matches = [s 
                                for s in self.options
                                if s and s.startswith(text)]
                logging.debug('%s matches: %s', repr(text), self.matches)
            else:
                self.matches = self.options[:]
                logging.debug('(empty input) matches: %s', self.matches)

        # Return the state'th item from the match list,
        # if we have that many.
        try:
            response = self.matches[state]
        except IndexError:
            response = None
        logging.debug('complete(%s, %s) => %s', 
                      repr(text), state, repr(response))
        return response