Any better way for doing a = b + a?

后端 未结 1 1475
礼貌的吻别
礼貌的吻别 2021-01-05 23:45

I\'m using PyCharm and I have this statement:

a = \'foo\'
b = \'bar\'
a = b + a

and PyCharm highlights the last line saying that:

相关标签:
1条回答
  • 2021-01-06 00:45

    Just ignore PyCharm, it is being obtuse. The remark clearly doesn't apply when the operands cannot just be swapped.

    The hint works for numeric operands because a + b produces the same result as b + a, but for strings addition is not commutative and PyCharm should just keep out of it.

    If you really want to avoid the message, you could use string formatting:

    a = '{}{}'.format(b, a)
    

    but I'd not bother, really.

    0 讨论(0)
提交回复
热议问题