Say I have the following class with a method returning a list:
class C: def f(self): return [1,2,3]
If I loop over the list returned fro the method, as follows:
c = C() for i in c.f(): print(i)
Inside the for
loop, will c.f()
be executed multiple times? If yes, in order to get it once, do I have to do assignment outside of the loop, or there is some trivial way?
Advertisement
Answer
In [395]: def tester(): ...: print "Tester Called!" ...: return [1,2,3] In [396]: for i in tester(): ...: pass Tester Called!
Seems the answer is no.