+,+=,append,extend of the Python list

Source: Internet
Author: User

One of the interview questions.

def func1 (P):    p = p + [1]def Func2 (P): P + = [    1]p1 = [1,2,3]P2 = [1,2,3]func1 (p1) Func2 (p2) print p1print p2
Results:

I thought it was a local variable like this. Because it does not affect the external list. So the answer should be P1 =[1,2,3], p2=[1,2,3], however


Re-tortured by the interviewer, I'm not reconciled. I looked up a python local variable:

x = [1,2,3]def func (x): print "local! Original x = ", x x = [1] print" local! Now x = ", Xfunc (x) print" global! x = ", X
Results:

local! Original x = [1, 2, 3]local! Now x = [1]global! x = [1, 2, 3]
Oh, yes. I remember having to use global variables to add a statement such as Globals X.


For the sake of insurance, add an ID (), check to see if the object is the same first:

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func (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)
Results:
Before Func (), global! x =  [1, 2, 3] ID (x) =  46798728in func (), local! original x =  [1, 2, 3] ID (x) =  46798728in func (), local! n ow x =  [1] ID (x) =  46789512after func (), global! x =  [1, 2, 3] ID (x) =  46798728

Well, you can see that the ID in the global variable (x) = 46798728,x into Func (), and because it runs x = [1], it becomes the ID (x) = 46789512. Reasonable

This also shows that Python is indeed a reference-to-function. (and then the egg)


Using the ID (x), look at the next x = x + [1] Object how it changes:

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func (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)
Results:

Before Func (), global! x =  [1, 2, 3] ID (x) =  46339976in func (), local! original x =  [1, 2, 3] ID (x) =  46339976in func (), local! n ow x =  [1, 2, 3, 1] ID (x) =  46390664after func (), global! x =  [1, 2, 3] ID (x) =  46339976
Ah. x = x + [1], is a new object, ID (x) = 46390664.


using the ID (x), see how the next X + = [1] object is changing :

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func (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)
Results:

Before Func (), global! x =  [1, 2, 3] ID (x) =  46536584in func (), local! original x =  [1, 2, 3] ID (x) =  46536584in func (), local! n ow x =  [1, 2, 3, 1] ID (x) =  46536584after func (), global! x =  [1, 2, 3, 1] ID (x) =  46536584
Ah, the ID (x) is the same throughout. x + = [1],python directly on the original object, is really lazy enough to say.


Use the ID (x) to see how the next X.append ([1]) object changes :

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func (x):    print" in Func (), local! Original x = ", X," id (x) = ", id (x)    x.append ([1])    print" in Func (), local! Now x = ", X," id (x) = ", id (x) func (x) print" After Func (), global! x = ", X," id (x) = ", ID (x)
Results:

Before Func (), global! x =  [1, 2, 3] ID (x) =  47191944in func (), local! original x =  [1, 2, 3] ID (x) =  47191944in func (), local! n ow x =  [1, 2, 3, [1]] ID (x) =  47191944after func (), global! x =  [1, 2, 3, [1]] ID (x) =  47191944
Ah, ID (x) All the way, it seems that the list of property methods are working on the original object, I remember List.sort () is also, will be verified list.extend () is expected to be.



Use the ID (x) to see how the next X.extend ([1]) object changes :

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func (x):    print" in Func (), local! Original x = ", X," id (x) = ", id (x)    x.extend ([1])    print" in Func (), local! Now x = ", X," id (x) = ", id (x) func (x) print" After Func (), global! x = ", X," id (x) = ", ID (x)
Results:

Before Func (), global! x =  [1, 2, 3] ID (x) =  48437128in func (), local! original x =  [1, 2, 3] ID (x) =  48437128in func (), local! n ow x =  [1, 2, 3, 1] ID (x) =  48437128after func (), global! x =  [1, 2, 3, 1] ID (x) =  48437128
Sure enough, the ID (x) is the same as the whole.





Say List.append () is append, Extend () is to expand, their difference is probably:

>>> a = [1,2,3]>>> b = [4,5,6]>>> c = [7,8,9]>>> A.append (b) >>> a[1, 2, 3, [4 , 5, 6]]>>> c.extend (b) >>> c[7, 8, 9, 4, 5, 6]>>>
Looking at some of the above code, you should also be able to see the smart:

List1 + = List2 equivalent to List1.extend (LIST2), which is evidence :

SOURCE Address: Http://svn.python.org/view/python/trunk/Objects/listobject.c?

View=markup

913static pyobject *914list_inplace_concat (pylistobject *self, Pyobject *other) 915{916    pyobject *result;917918    result = Listextend (self, other);//+= sure used Listextend () 919    if (result = = NULL) 920        return result;921    Py _decref (result); 922    py_incref (self); 923    return (Pyobject *) self;924}


Take advantage of the ID (x). Look under Global x below. Change the object:

x = [1,2,3]print] before func (), global! x = ", X," id (x) = ", id (x) def func ():    Global 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 () print" after Func (), global! x = ", X," id (x) = ", ID (x)
Results:

Before Func (), global! x =  [1, 2, 3] ID (x) =  47781768in func (), local! original x =  [1, 2, 3] ID (x) =  47781768in func (), local! n ow x =  [1, 2, 3, 1] ID (x) =  47795720after func (), global! x =  [1, 2, 3, 1] ID (x) =  47795720
Well, global guarantees that even if my variable x points to the object in the function, the outer x points to the new object.


Back to interview questions:

def func1 (P):    p = p + [1]def Func2 (P): P + = [    1]p1 = [1,2,3]P2 = [1,2,3]func1 (p1) Func2 (p2) print p1print p2

P1 incoming func1 (). Because of the + operation, a new object is generated. But there is no return to the external P1. So the external p1=[1,2,3].

P2 incoming Func2 (), because the + = operation, is List.extend (). Operation. Operation on the original object. So p2=[1,2,3,1].



Under The Spit Groove:

The fact that Python is referenced in a function is a reference, assuming that an object, obj, goes into the function and is changed, then the obj is changed wherever it is. There are no copies or anything.

Then why does it sometimes look like. How obj is changed in the function, the external obj is feuds, ah, because this changed obj is not the original.

For example x = x + [1]. Is the new x really the x that was originally passed in? No.

The x at this point is the new object (see ID). The x that was previously passed in. has not been changed.

A little humble opinion, seek to fight face.



Summarize:

1. List + creates a new object.

2, the list of + + and list.extend (), equivalent. Are all operating on the original object.

3, List.append (). Also operates on the original object.

4, global variables, well, good (this is what summary).











+,+=,append,extend of the Python list

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.