** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
list_augment
def test_func(x, l = []):
l.append(x)
return l
r = test_func(100)
print(r)
r = test_func(100)
print(r)
result
[100]
[100, 100]
I often use a new list every time, In such a case, if you set an empty list as the default argument, You will continue to use one list forever.
list_augment
def test_func(x, l = None):
#Initialize l to an empty list
if l is None:
l = []
l.append(x)
return l
r = test_func(100)
print(r)
r = test_func(100)
print(r)
result
[100]
[100]
In def
, write to initialize l
first.
Then, every time you call test_func
, l
will be initialized, so
You will be able to use an empty list every time.
Recommended Posts