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

Source: Internet
Author: User

+ = is an upgraded version of +, with the ability to write back the executed results to the variable itself, the variable variable itself has a more magical method than the immutable variable _iadd_, + = Operation call _iadd_method, when there is no method, try to call the _add_method, immutable variable No _iadd_Method

Example:

# A + = b>>> a1 = Range (3)>>> A2 = A1>>> A2 + = [3]>>> a1[0, 1, 2,  3]>>> a2[0, 1, 2, 3]# a = a + B> >> a1 = range (3) >>> a2 = A1 >>> a2 = a2 + [3]>> > A1[0, 1, 2] >>> a2[0, 1, 2, 3]          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
a1 = [0, 1, 2]a1 += [3] <==> a1.__iadd__([3]) print(a1) # [0, 1, 2, 3]
    • 1
    • 2
    • 3

The _iadd_ method is updated directly on the original object A1, and the return value of the method is None
+ Action Call the Add method

a1 = [0, 1, 2]a1 = a1 + [3] <==> a1 = a1.__add__([3])
    • 1
    • 2

The _add_ method returns a new object, and the original object is not modified because the A1 is re-assigned, and A1 points to a new object

To raise another chestnut, can you judge and understand the output of the following?:

+=
in [1]: def selfadd (a):...: A + = a...: print (a)...: in [2]: A_int =1# incoming immutable variable in [3]: Selfadd (a_int) Span class= "Hljs-number" >2# immutable variable itself has not changed in [4]: a_intout[< Span class= "Hljs-number" >4]: 1in [6]: A_li = [1,2]# incoming mutable variable in [7]: Selfadd (A_li) [1, 2, 1, 2]# + =, variable variable content changed in [8]: A_liout[8": [1, 2, 1, 2]             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
=+
in [5]: def selfAdd2 (a):  ...: a = a+a  ...: print (a) ...:# passed immutable variable in [10]: selfAdd2 (a_int) 2 # as expected did not change in [11]: A_intout[11]: Span class= "Hljs-number" >1# incoming mutable variable in [12]: SELFADD2 (A_li) [ Span class= "Hljs-number" >1, 2, 1, 2] # =+, no change occurred in [13]: A_liout[13]: [ 1, 2]          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

Also note here:

The function parameter in Python is a reference pass (not a value pass). For an immutable type, the dependent variable cannot be modified, so the operation does not affect the variable itself, and for a mutable type, the operation in the body of the function may change the passed parameter variable.

Example:

in [51]: def Count ():  ...:  For i in range (1,4):  ...: def f ():  ...: return i*i  ...: Fs.append (f) return FS  ...: for F in count (): ...:999          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

First, you can see that the printing results are all 9, the application of the transfer function or call address (here Python is a high-level language, mainly for the development of applications, in fact, not to investigate the underlying)
Second, the function in the Declaration and definition of the process, and did not save the previous I value, after the end of the loop, the function remains only the last pass in the value: 3
Third, therefore, the loop calls the function, each printing is the result of 3*3:9

      • Variable type, the value can be changed:
        • Lists List
        • Dictionary Dict
      • Immutable types, values cannot be changed:
        • numeric type int, long, bool, float
        • String str
        • Tuple tuples
        • Like friends can add QQ Group, the group has free information for everyone to Exchange learning Oh!!!

The difference between a+=a and a=a+a 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.