Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Rule] Use functions from operator #158

Open
TomFryers opened this issue Oct 16, 2022 · 0 comments
Open

[New Rule] Use functions from operator #158

TomFryers opened this issue Oct 16, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@TomFryers
Copy link

TomFryers commented Oct 16, 2022

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 TomFryers added the enhancement New feature or request label Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
2 participants