06.12.19 07:38, Steven D'Aprano пише:
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.

     for item in jsonld_items:
         if item['@type'] == 'Product':
             return item
         else:  # Even this "else" is redundent too.
             return {}

     for item in jsonld_items:
         if item['@type'] == 'Product':
             return item
     # else is unnecessary here
     return {}

In real code this can be a fragment of the larger function and look like:

        for item in jsonld_items:
            if item['@type'] == 'Product':
                break
        else:
            item = {}


The name says it returns an item, but the default is to return an empty
dict, which seems like an unusual choice for the "default product where
no product is specified". I would have guessed None if the product is
missing.

I would return None, but I do not know the context. It is obvious that jsonld_items is a list of dicts, so returning an empty dict can be more reasonable in the particular code. At least the result always has the same type.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JT3J5QEJZ6HY4NY7DV6EIR7L6NCMDFDR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to