On 12/05/2019 01:40 PM, 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 {}


My argument is that the intent is clearer in:

    def get_product_item(jsonld_items):
         return first((item for item in jsonld_items if item['@type'] == 
'Product'), {})

I haven't followed the main thread, but I can unequivocally state that for-loop 
version is easier to read.  The only thing missing to make the intent crystal 
clear is one more word in the function name:

    def get_first_product_item(...):

Also, the for-loop version quits the moment it finds a Product type, while the 
`first` version has to first process the entire jsonld_items structure.

--
~Ethan~
_______________________________________________
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/D4OSWMKT5KRTUG6SDOTJW55UAON3E4QB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to