Skip to content Skip to sidebar Skip to footer

Issue With Iterating Through List Of Callable

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 ca

Solution 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)

Post a Comment for "Issue With Iterating Through List Of Callable"