syntax - What does += mean in Python? -


I see the code in Python for example like this:

  if cnt & Gt; 0 and lane (AST)> 1: While the CNT & gt; 0: aStr = aStr [1:] + aStr [0] cnt + = 1  

+ = What do you mean?

a + = b essentially a = a + B , besides:

  • + always returns a new assigned object, but the + = should be the object In-place if it is unstable (such as list or dict , but int and str is irreversible).
  • Assessed twice in a = a + b , a


If this is the first time that you have to face the + = operator, you may be surprised Here's an example:

  # two variables referring to the same list> & gt; Why it might be that it can modify the object instead of creating a new one. & Gt; List 1 = []> & Gt; & Gt; List2 = list1 # + = Modifies the object indicated by list1 and list2 & gt; & Gt; & Gt; List 1 + = [0] & gt; & Gt; & Gt; List 1, List 2 ([0], [0]) # + creates a new, independent object & gt; & Gt; & Gt; List 1 = []> & Gt; & Gt; List2 = list1 & gt; & Gt; & Gt; List1 = list1 + [0] & gt; & Gt; & Gt; List 1, List 2 ([0], [])  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -