Issue with iterating through list of callable

2019-08-03 23:24发布

问题:

I am having an issue with iterating over a list of callables in python. The callables are supposed to be called on a generator of strings. The current behaviour is that the last callable in the list is called as many times as there are callables in the list. My current code:

for m in list_of_callables:
    strings = (m(s) for s in strings)

In the above code strings is initially of type 'Generator'. I have also tried the following:

for i in range(len(list_of_callables)):
    strings = (list__of_callables[i](s) for s in strings)

This has not worked either, but when I don't loop over the callables and simply call them it works just fine:

strings = (list_of_callables[0](s) for s in strings)
strings = (list_of_callables[1](s) for s in strings)

This seems strange to me as the above should be equivalent to the for loop.

Thanks in advance for your help and suggestions :).

回答1:

strings = (m(s) for s in strings)

This doesn't actually call your callable. It creates a generator expression that will call m later, using whatever m happens to be later.

After the loop, m is the final callable. When you try to retrieve an element from strings, all those nested genexps look up m to compute a value, and they all find the last callable.

You could fix this by using itertools.imap instead of a genexp:

strings = itertools.imap(m, strings)