On Fri, Dec 6, 2019, 12:47 AM Steven D'Aprano
On Thu, Dec 05, 2019 at 05:40:05PM -0400, Juancarlo Añez wrote:
I just found this code:
def get_product_item(jsonld_items): for item in jsonld_items: if item['@type'] == 'Product': return item else: return {}
I'm sorry, I can't tell what that is supposed to do. Is the "return {}" supposed to be inside the loop? If so, it has been accidentally dedented. Is it meant to be outside the loop? The "for-else" is redundent, since there is no break.
"return", like "break", causes the "else" suite to be skipped. https://docs.python.org/3/reference/compound_stmts.html#the-for-statement does not clearly specify this; it only says that the else suite is executed when the iterator is exhausted or empty, and that "break" skips it. Perhaps a sentence should be added to clearly and unambiguously state that "return" skips it also?