-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
[Enhancement]: Rule to prefer in-place operators #310
Comments
Thank you for opening this! This sounds easy enough to implement, I'll give it a shot over the weekend. Because anyone can make their own custom in-place operators, I think it is best to only support built-in types, though there should be an option to extend this to all types once I add option support (see #100). Also, I'd probably add these to your list as well: a = a or b
c = c and d Re-write as: a |= b
c &= d assuming |
Hi, I'm sure you are considering it, but allow me this warning. For mutable types, inplace operators are not equivalent to normal operators for exemple: a=[]
b=a
a+= [1]
a= a+[2]
print(a,b) # [1,2] [1] |
Overview
In-place operators lead to more concise code that is still readable. I'm not sure if there's any objective drawbacks (like a common pitfalls for certain types). And performance-wise my understanding is that it should be faster (due to the in-place nature) or equivalent (thanks to Python duck-typing that will fallback to
__add__
if__iadd__
is not implemented). Relates to #28Proposal
Any of the following:
Could be re-written as:
The text was updated successfully, but these errors were encountered: