I have some function which calls itself:
def rep_func(parent_id, account_id):
...
for page in iterator:
for item in page['Children']:
if id == item['Id']:
//return parent_id
for page in org_iterator:
for ou in page['Children']:
rep_func(ou['Id'], account_id)
I need to return the parent_id
from the function when the if
is succesfull. How can I do this? Do I need a break
?
CodePudding user response:
You need to return
the value; this automatically breaks the current loop.
For the recursive case, I'm guessing you want to check whether any value was found, and return it conditionally, so that the loop will keep searching if the recursive call was unsuccessful:
def rep_func(parent_id, account_id):
# ...
for page in iterator:
for item in page['Children']:
if id == item['Id']:
return parent_id
for page in org_iterator:
for ou in page['Children']:
if result := rep_func(ou['Id'], account_id):
return result
# we implicitly return None if no match found