This question already has an answer here:
- Any better way for doing a = b + a? 1 answer
I'm using PyCharm (version 4.0.3) and getting a style warning Assignment can be replaced with augmented assignment on the second line of the following code*:
abc = 'and cheese'
abc = 'ham' + abc
* - My code is not really this code but it generates the same error. I am programmatically generating two strings and I have to / would like to generate the first line (second part of the English syntax) before the second line (the first part of the English syntax).
But I don't know what augmented assignment could do this. If the code were like this (where the first part of the desired final string could be generated first in execution order)
abc = 'ham'
abc = abc + 'and cheese'
then I believe the problem is trivially resolved with the += operator:
abc = 'ham'
abc += 'and cheese'
But in the context of my problem (where the 'and cheese' part is declared before 'ham'), is there a way to satisfy this warning?
There are more than one way to skin a cat (or to concatenate strings).
You can concatenate using
str.join
but it may be marginally less efficient for small lists:Or using format:
But really, the correct way to silence the warning is to submit a bug report for the IDE, because it looks like there is nothing wrong with your code (someone in the comments already pointed out it is not reproducible on the last version).