I have two ways to loop data, one using with for loop another with yield, I want to know what’s the difference.
-
For Loop(get 100000 data)
data='select 100000 data from database' for d in date: do something with d -
yield (get 1000 data every time,then query database 100 times)
def func(): data='select 1000 data from database' while date.count>0: yield data data = func() for d in data: do something with d
Functionally, they’re the same.
The main difference is that, in the second case, it’s really easy to have several different “do something” blocks without repeating the SQL statement. In the first case, the “do something” is hard-coded.