我正在尝试在代码中运行Multiple for Loops。由于某些原因,它的行为异常。
这里是方法:我叫function1()
def function1():
all_cohorts = get_all_cohorts()
for chrt in all_cohorts:
emit_event()
#end of for loop
# this code should be called after the for loop is done
call_for_members_of_cohort(all_cohorts)
def call_for_members_of_cohort(all_cohorts):
for chrt in all_cohorts:
get_members_of_cohort(chrt)
def get_members_of_cohort(cohort):
users = cohort.users.all()
for u in users:
emit_event()
这是方法的整个流程。由于某些原因,循环运行2次。例如call_for_members_of_cohort
函数调用了2个同类群组,而get_members_of_cohort
将运行2次。但它可以运行2次以上。
该方法有什么问题吗?有人可以建议我解决这个问题还是建议我采用另一种方法?