The effect of default parameters in functions of python notes

# Declare a function, the first parameter is an integer, the second parameter is a list type,
# l has a default value, the default value is [] an empty list

def f(x,l=[]):
    for i in range(x):
        l.append(i*i)
    print(l)

# f(2) = f(2,l=[])

f(2)
# output what [0,1]
f(3,[3,2,1])
# [3,2,1,0,1,4]
f(3) # l also records the above value
f(3,l=[]) # Try to write like this
# [0,1,4]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483258&siteId=291194637