dabeaz-course/practical-python

itemgetter instead of def/lambda

Closed this issue · 1 comments

Hi

thanks for offering this course online. i've learned a ton!

on https://github.com/dabeaz-course/practical-python/blob/main/Notes/07_Advanced_Topics/02_Anonymous_function.md

this example:

def stock_name(s):
    return s['name']

portfolio.sort(key=stock_name)

can also be implemented using itemgetter:

from operator import itemgetter
portfolio.sort(key=itemgetter('name'))

see https://docs.python.org/3/howto/sorting.html#operator-module-functions
and https://docs.python.org/3/library/operator.html#operator.itemgetter

i know it kinda defeats the purpose of what you're trying to show, but it's an alternative to keep you from writing one-liners.

maybe include both?

There's only so much information one can put in a course before heads explode. Lambda is a more general (and important) idea than using itemgetter.