Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String equivalent of =+ but existing string as appended rather than prepended to new string [duplicate]

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?

like image 734
BKay Avatar asked Jan 09 '15 17:01

BKay


1 Answers

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:

abc = " ".join((abc, 'and cheese'))

Or using format:

abc = "{} {}".format(abc, 'and cheese')

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).

like image 188
Paulo Scardine Avatar answered Sep 24 '22 22:09

Paulo Scardine