你在python代码中在哪里使用生成器功能?

sys*_*out 5 python generator

我已经研究了生成器功能,我想我得到了它,但我想了解我可以在我的代码中应用它的位置.

我想到了以下我在"Python必备参考"一书中读到的例子:

# tail -f
 def tail(f):
  f.seek(0,2) 
  while True:
   line = f.readline() 
   if not line: 
     time.sleep(0.1)
     continue
   yield line
Run Code Online (Sandbox Code Playgroud)

你有没有其他有效的例子,其中发电机是最好的工具,如tail -f?

您经常使用生成器功能以及通常应用哪种功能\部分程序?

Nic*_*kis 6

当我实现扫描程序(tokenizer)或迭代数据容器时,我经常使用它们.

编辑:这是我用于C++语法高亮程序的演示标记器:

whitespace = ' \t\r\n'
operators = '~!%^&*()-+=[]{};:\'"/?.,<>\\|'

def scan(s):
    "returns a token and a state/token id"
    words = {0:'', 1:'', 2:''} # normal, operator, whitespace
    state = 2 # I pick ws as first state
    for c in s:
        if c in operators:
            if state != 1:
                yield (words[state], state)
                words[state] = ''
            state = 1
            words[state] += c
        elif c in whitespace:
            if state != 2:
                yield (words[state], state)
                words[state] = ''
            state = 2
            words[state] += c
        else:
            if state != 0:
                yield (words[state], state)
                words[state] = ''
            state = 0
            words[state] += c
    yield (words[state], state)
Run Code Online (Sandbox Code Playgroud)

用法示例:

>>> it = scan('foo(); i++')
>>> it.next()
('', 2)
>>> it.next()
('foo', 0)
>>> it.next()
('();', 1)
>>> it.next()
(' ', 2)
>>> it.next()
('i', 0)
>>> it.next()
('++', 1)
>>> 
Run Code Online (Sandbox Code Playgroud)