The "pit" and "pit" when a list is set as a parameter in a Python function

Source: Internet
Author: User

In the Python function, if the passed parameter is a list by default , then be aware that there is a pit here!!

Into the pit
def F (x,li=[]):    for in  Range (x):        li.append (i*i)     printprint('---1---') F (4)  Print('---2---') F (5)
Expected results
---1---1, 4, 9]---2---1, 4, 9, 16]
Execution results
---1---1, 4, 9]---2---1, 4, 9, 0, 1, 4, 9, 16]
Out of the Pit

When the function is defined, the value of the default parameter list in the function is saved, which is the list li=[];

If a new list is passed at each invocation, the passed list is used, no pass, and the default parameter (li=[] is saved when the function is defined);

In the above two calls, no new list is passed (using the default list li=[]), and the program invokes the default parameters ((li=[]) that are saved when the function is defined.

When the list is append, it will append append value on the li=[] original, so the result will be generated.

identification by the ID of the printed list

Print List li=[] ID:

def F (x,li=[]):    print(ID (li))  # Add print ID    for in Range (x):        li.append (i*i)    print(li)  print( ' ---1--- ' ) F(4)print('---2---') F (5)

Results:

---1---1403061239062481, 4, 9]---2---1403061239062481, 4, 9, 0, 1, 4, 9, 16]

Will find the ID value is the same;

Describes the default parameters used when defining a function when executing two times li=[]

Upload a new list in the execution

The ID of the print List li=[] and the ID of the new list passed in:

defF (x,li=[]):    Print(ID (LI)) forIinchRange (x): Li.append (i*i)Print(LI)Print('---1---') F (4)Print('---2---') F (5,[])Print('---3---') F (6)

Results:

---1---1, 4, 9]---2---1, 4, 9, +]---3---1, 4, 9, 0, 1, 4, 9, 16, 25]

You will find that a function that passes the empty (new) list does not print the same ID as it was passed;

When an empty list is passed, the empty list passed in the function body is used, and the function default value li=[] is not passed, so this result is generated.

Optimization

If you want to achieve the desired result, simply make a judgment in the function body:

defF (x, li=[]):    if  notLi:#If Li is not empty then go down (empty list);Li = []     forIinchRange (x): Li.append (i*i)Print(LI)Print('---1---') F (4)Print('---2---') F (5)Print('---3---') F (6)

Results:

---1---1, 4, 9]---2---1, 4, 9, +]---3---1, 4, 9, 16, 25]

The "pit" and "pit" when a list is set as a parameter in a Python function

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.