The difference between a=a+2 and a+=2 in Python

Source: Internet
Author: User

1, A=a+2, represents a new object, the new object name is still a, but the point of memory address has changed

>>> a=2>>> ID (a)140406287260016>>> a=a+2>>> a4> >> ID (a)140406287259968

So you can do this for a tuple object (immutable object).

>>> tuple1= >>> ID (tuple1)4521580448>>> tuple1=tuple1+ ( 3,)>>> tuple1 (1, 2, 3)>>> ID (tuple1)4521658880

2. a+=2 for some objects the operation is to represent the original object, the operation of some objects is to generate a new object

Immutable Object Tuple1, after the operation, the memory address has changed, generating a new object
>>> tuple1= >>> type (tuple1)'tuple'>> >> tuple1+= (3,)>>> ID (tuple1)4521658880>>> tuple1+= (4,5)> >> ID (tuple1)4520649072

The list object, variable object, + = operation, append operation, and extend operation are all operated on the original object.

>>> list1=[1,2]>>>ID (list1)4521614656>>> list1+=[3]>>>ID (list1)4521614656>>> List1.append (4)>>>ID (list1)4521614656>>> List1.extend (5) Traceback (most recent): File"<stdin>", Line 1,inch<module>TypeError:'int'Object is  notiterable>>> List1.extend ([5])>>>ID (list1)4521614656>>>

3.

x = [A]  Print "before Func (), global! x ="X"ID (x) =", ID (x)deffunc ():GlobalxPrint "in Func (), local! original x ="X"ID (x) =", ID (x) x= x + [1]      Print "in Func (), local! now x ="X"ID (x) =", ID (x) func ()Print "After func (), global! x ="X"ID (x) =", ID (x) Result: [Python] View plain copybefore func (),Global! x = [1, 2, 3] ID (x) = 47781768inchFunc (), local! Original x = [1, 2, 3] ID (x) = 47781768inchFunc (), local! Now x = [1, 2, 3, 1] ID (x) = 47795720After func (),Global! x = [1, 2, 3, 1] ID (x) = 47795720

GlobalThis ensures that even if my variable x points to the object in the function, the outer x points to the new object.

x = [A]  Print "before Func (), global! x ="X"ID (x) =", ID (x)deffunc (x):Print "in Func (), local! original x ="X"ID (x) =", ID (x) x= x + [1]      Print "in Func (), local! now x ="X"ID (x) =", ID (x) func (x)Print "After func (), global! x ="X"ID (x) =", ID (x) Result: Before Func (),Global! x = [1, 2, 3] ID (x) = 46339976inchFunc (), local! Original x = [1, 2, 3] ID (x) = 46339976inchFunc (), local! Now x = [1, 2, 3, 1] ID (x) = 46390664After func (),Global! x = [1, 2, 3] ID (x) = 46339976

x = x + [1], is a new object, ID (x) = 46390664

Using the ID (x), view the next X + = [1] How does the object change: x= [A]  Print "before Func (), global! x ="X"ID (x) =", ID (x)deffunc (x):Print "in Func (), local! original x ="X"ID (x) =", ID (x) x+ = [1]      Print "in Func (), local! now x ="X"ID (x) =", ID (x) func (x)Print "After func (), global! x ="X"ID (x) =", ID (x) Result: Before Func (),Global! x = [1, 2, 3] ID (x) = 46536584inchFunc (), local! Original x = [1, 2, 3] ID (x) = 46536584inchFunc (), local! Now x = [1, 2, 3, 1] ID (x) = 46536584After func (),Global! x = [1, 2, 3, 1] ID (x) = 46536584
ID (x) as a whole, x + = [1],python directly on the original object

Reference:

1, http://blog.csdn.net/emaste_r/article/details/47395055

The difference between a=a+2 and a+=2 in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.