Say I have the following class with a method returning a list:
JavaScript
x
4
1
class C:
2
def f(self):
3
return [1,2,3]
4
If I loop over the list returned fro the method, as follows:
JavaScript
1
4
1
c = C()
2
for i in c.f():
3
print(i)
4
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
JavaScript
1
8
1
In [395]: def tester():
2
print "Tester Called!" :
3
return [1,2,3] :
4
5
In [396]: for i in tester():
6
pass :
7
Tester Called!
8
Seems the answer is no.