Home >Backend Development >Python Tutorial >Introduction to Python using lists as default parameters in functions (code example)

Introduction to Python using lists as default parameters in functions (code example)

不言
不言forward
2019-01-28 10:22:402879browse

This article brings you an introduction to using lists as default parameters in Python functions (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

This article will introduce a pitfall of Python that the author encountered at work, which is using lists as default parameters.

We know that in Python, a list (list) is a mutable object, so the contents of the list may change within the function. Another thing to note is how the contents of the list change when using a list as the default parameter of a function.

First, let’s look at the following code example:

def add(x, lst=[]):
    if x not in lst:
        lst.append(x)

    return lst

def main():
    list1 = add(1)
    print(list1)

    list2 = add(2)
    print(list2)

    list3 = add(3, [11, 12, 13, 14])
    print(list3)

    list4 = add(4)
    print(list4)

main()

You may think that the output result will be:

[1]
[2]
[11, 12, 13, 14, 3]
[4]

But in fact, the output result of this program is:

[1]
[1, 2]
[11, 12, 13, 14, 3]
[1, 2, 4]

Why is this? The function of the function add is to append x to the list lst when x is not in the list. When the function is executed for the first time, the default value [] of the parameter lst is created. This default value will only be created once. add(1) adds 1 to lst. When the function is called again, lst is [1] instead of [] because lst is only created once. When the lst of the parameter is [11,12,13,14], the lst is [11,12,13,14]. When list4 calls the function, the default parameters are used, so the default parameter lst is now [1,2].
  In order to better understand the calling situation, you can output the id of lst in the add function, such as the following code:

def add(x, lst=[]):
    print(id(lst))

    if x not in lst:
        lst.append(x)
    
    return lst

def main():
    list1 = add(1)
    print(list1)

    list2 = add(2)
    print(list2)

    list3 = add(3, [11, 12, 13, 14])
    print(list3)

    list4 = add(4)
    print(list4)

main()

The output result is as follows:

4469603648
[1]
4469603648
[1, 2]
4469670472
[11, 12, 13, 14, 3]
4469603648
[1, 2, 4]

You can see, list1, list2 , the id of the default parameter has not changed when list4 is called, but the id of list3 has changed.
 This is a pitfall of Python using lists as default parameters. So, how to avoid stepping into pitfalls? If you want to use the default list which is [] every time the function is called, you can modify the function parameters like the following program:

def add(x, lst=None):

    if lst is None:
        lst = []
    if x not in lst:
        lst.append(x)

    return lst

def main():
    list1 = add(1)
    print(list1)

    list2 = add(2)
    print(list2)

    list3 = add(3, [11, 12, 13, 14])
    print(list3)

    list4 = add(4)
    print(list4)

main()

The output result is as follows:

[1]
[2]
[11, 12, 13, 14, 3]
[4]

The above is the detailed content of Introduction to Python using lists as default parameters in functions (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete