flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

[New Rule] Use functions from operator

Open TomFryers opened this issue 1 year ago • 0 comments

Explanation

References (and function calls in some cases) are about as simple as you can get. Lambdas aren't too bad, but a bit more complicated. Functions from operator can also be faster than equivalent lambdas.

I feel that these are most common for itemgetter, attrgetter and methodcaller.

Example

# Bad
lambda a, b: a < b
lambda a: not a
lambda a: a.fish
lambda x: x[0]
lambda x: (x[0], x[3])


# Good
operator.lt
operator.not_
operator.attrgetter("fish")
operator.itemgetter(0)
operator.itemgetter(0, 3)

TomFryers avatar Oct 16 '22 14:10 TomFryers