MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
187 stars 19 forks source link

[New Rule] Use functions from operator #158

Open TomFryers opened 2 years ago

TomFryers commented 2 years ago

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)