"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."
The first version doesn't have to process the whole structure; it's written with a generator expression, so it only tests and produces values on demand, and next stops demanding them as soon as it gets a single result. On Thu, Dec 5, 2019 at 10:13 PM Ethan Furman <[email protected]> wrote: > 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 -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/D4OSWMKT5KRTUG6SDOTJW55UAON3E4QB/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/33UBKWPZLKUDD6HEYZ46737KRQNQICOD/ Code of Conduct: http://python.org/psf/codeofconduct/
