More and more Python features are moving into a "lazy executable" such as an expression generator and other iterators. Sometimes, however, I see that I want to drop one βforβ circuit in order to perform some action.
What would be the most pythonic thing to get a loop actually executed?
For instance:
a = open("numbers.txt", "w") (a.write ("%d " % i) for i in xrange(100)) a.close()
Do not activate the code, but you understand what I mean. If I use a list generator, instead I have the side effect of creating an N-length list filled with "No".
I am currently using an expression as an argument in a call to "any" or "all". But I would like to find a way that will not depend on the result of the expression being executed in the loop - both "any" and "all" can stop depending on the expressed expression.
To be clear, these are the ways to do this that I already know about, and each has its own drawbacks:
[a.write ("%d " % i) for i in xrange(100))] any((a.write ("%d " % i) for i in xrange(100))) for item in (a.write ("%d " % i) for i in xrange(100)): pass
source share