"Making changes to existing code that works" is how programs evolve;-). Write a good battery of tests that give known results with the existing code, save those results (that's normally known as "golden files" in a testing context); then make the changes, rerun the tests, and verify (ideally in an automated way) that the only changes to the tests' results are those that are specifically intended to be there -- no undesired or unexpected side effects. One can use more sophisticated quality assurance strategies, of course, but this is the gist of many "integration testing" approaches.
As for the two ways to write simple key=
function, the design intent was to make operator.attrgetter
faster by being more specialized, but at least in current versions of Python there's no measurable difference in speed. That being the case, for this special situation I would recommend the lambda
, simply because it's more concise and general (and I'm not usually a lambda-lover, mind you!-).