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

Source: Internet
Author: User
Tags python list

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 like this kind of argument as a local variable, because it will not affect the external list, so the answer should be P1 =[1,2,3], p2=[1,2,3], however


I was tortured by the interviewer again. 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]
Yes, I remember having to use global variables to add a statement like globally 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 (), because x = [1] is executed before it becomes 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, ID (x) all the way, x + = [1],python directly on the original object to operate, is really lazy 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, the list.extend () estimate to be verified is also.



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 code 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}


Using the ID (x), look at the object changes under Global x:

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 the + operation generates a new object, but no return to the external P1, so the external p1=[1,2,3].

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



Under The Spit Groove:

In fact, the parameters of Python in the function are referenced, if an object obj into the function, is changed, then no matter where the obj is changed, and there is no copy of what.

So why does it sometimes seem that obj is changed in the function, and that the external obj is feuds, ah, because the 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), and the x that was passed in previously is not 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 on the original object operation.

3, List.append (), but also on the original object operation.

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











Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.