且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 生成器行为

更新时间:2022-06-06 23:38:17

http://docs.python.org/reference/expressions.html#generator-expressions

生成器表达式中使用的变量在以下情况下被延迟计算为生成器对象调用 __next__() 方法(在同一像普通发电机一样时尚).但是,最左边的 for 子句是立即评估,以便可以看到它产生的错误在处理生成器的代码中出现任何其他可能的错误之前表达.无法立即评估后续 for 子句因为它们可能依赖于前面的 for 循环.

Variables used in the generator expression are evaluated lazily when the __next__() method is called for generator object (in the same fashion as normal generators). However, the leftmost for clause is immediately evaluated, so that an error produced by it can be seen before any other possible error in the code that handles the generator expression. Subsequent for clauses cannot be evaluated immediately since they may depend on the previous for loop.

S[i:j+1] 在您执行生成器时进行评估,此时 S 具有最新值.

S[i:j+1] is evaluated when you execute the generator, and at that point S has the latest value.

您可以改用普通的生成器.现在 sssubgen 来说是本地的:

You can use a normal generator instead. Now ss is local to subgen:

import itertools

def subgen(ss):
    l=len(ss)
    for i in xrange(l):
        for j in xrange(i,l):
            yield ss[i:j+1]

subs=[]
for i in xrange(int(raw_input())):
    S=raw_input()
    subs.append(subgen(S))