Pass a list to a function __ function

Source: Internet
Author: User

When a list is passed to a function, because the list is a Mutable object, the contents of the list are returned to change after the function call.

def main ():
    x =1
    y = [1,2,4]
    m (x, y)
    print ("x is", x)
    print ("Y[0", Y[0])
def m (number, number s): Number
    = 1001
    numbers[0] = 5555

main ()
#运行结果
x is  1  
y[0] is 5555 #

When M is called, X remains 1, but y[0] is changed to 5555 because Y and numbers all point to the same list object. When M (X,y) is invoked, the reference values of x and Y are passed to number and numbers. Because y contains a reference value that points to a list, numbers now contains the same reference value that points to the same list. English number is immutable, so in a function that bain it creates an instance, and the original instance outside the function is not changed. Therefore, outside of the function x is still 1.

#以上照抄了书本原文, the truth understand, but not very thorough understanding, for the record.

To use a list as a default function

def add (x, LST = []):
    if x to LST:
        lst.append (x) return
    LST
def main ():
    List1 = Add (1) #lst default value [] is created, add (1) is added to LST
    print (list1)

    List2 = Add (2) #lst是 [1] instead of [],add (2) LST becomes [1,2]
    print (list2)

    List3 = Add (3, [One, one,]) #给出列表参数 [11,12,13,14] and pass this list to the LST
    print (list3)

    list4 =add (4) #默认列表参数被使用, because the default list is [ 1,2], so after calling Add (4), the default list becomes [1,2,4]
    print (LIST4)

main ()
#运行结果
[1]
[1, 2] 
[11, 12, 13, 14, 3]
[1, 2, 4]

If you want the default list to be [] each time the function is called, you can modify it to the following code

def add (x, LST = none): 
    if LST = = None:
        LST = []
    if x in LST:
        lst.append (x) return
    lst
D EF Main ():
    List1 = Add (1)
    print (list1)

    List2 = Add (2)
    print (list2)

    list3 = Add (3, [11, 12, 13, 1 4]
    print (LIST3)

    list4 =add (4)
    print (LIST4)

main ()
#运行结果
[1]
[2]
[11, 12 , 3]
[4]

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.