Hacker News new | past | comments | ask | show | jobs | submit login

Assuming the implementation has access to the "real" operator.itemgetter, I can't think of a case where lambda x: x['name'] cannot be replaced by a operator.itemgetter("name") and get a different result.

... except for the stack trace:

  >>> f = lambda x: x["A"]
  >>> f({"A": 3})
  3
  >>> f({"B": 3})
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 1, in <lambda>
  KeyError: 'A'

  >>> import operator
  >>> f = operator.itemgetter("A")
  >>> f({"A": 3})
  3
  >>> f({"B": 3})
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  KeyError: 'A'
So if 'x' implemented its own getitem and it inspected the stack (eg, how Pandas uses the 'level' parameter in 'eval' and 'query' methods to get locals() and globals()), then this change would cause different results.



Oh, I see.

I think you are right.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: