内容简介:本文将介绍笔者在工作中遇到的Python的一个坑,那就是使用列表作为默认参数。我们知道,在Python中,列表(list)是可变对象,所以列表的内容可能会在函数内改变。另一个需要注意的是,使用列表作为函数的默认参数时,列表的内容变化情况。首先,我们看以下的代码例子:
本文将介绍笔者在工作中遇到的 Python 的一个坑,那就是使用列表作为默认参数。
我们知道,在Python中,列表(list)是可变对象,所以列表的内容可能会在函数内改变。另一个需要注意的是,使用列表作为函数的默认参数时,列表的内容变化情况。
首先,我们看以下的代码例子:
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()
你可能以为输出的结果会是:
[1] [2] [11, 12, 13, 14, 3] [4]
但事实上,该程序输出的结果是:
[1] [1, 2] [11, 12, 13, 14, 3] [1, 2, 4]
这是为什么呢?函数add的功能是当x不在列表中时,将x追加给列表lst。当函数第一次执行时,参数lst的默认值[]被创建。这个默认值只会被创建一次。add(1)将1加到lst。当函数再次被调用时,lst是[1]而不是[],因为lst只被创建一次。当参数的lst为[11,12,13,14]时,lst就是[11,12,13,14]。list4调用函数时,使用默认参数,因此,现在默认参数lst为[1,2]。
为了更好地理解调用情况,可以在add函数中输出lst的id,如以下代码:
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()
输出结果如下:
4469603648 [1] 4469603648 [1, 2] 4469670472 [11, 12, 13, 14, 3] 4469603648 [1, 2, 4]
可以看到,list1, list2, list4调用时默认参数的id并没有发生改变,而list3的id发生了改变。
这是Python使用列表作为默认参数的一个坑。那么,如何避免踩坑呢?如果想要使用默认列表在每次函数调用时都是[],可以像下面的程序那样修改函数参数:
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()
输出的结果如下:
[1] [2] [11, 12, 13, 14, 3] [4]
本次分享到此结束,感谢阅读~
注意:本人现已开通微信公众号: Python爬虫与算法(微信号为:easy_web_scrape), 欢迎大家关注哦~~
以上所述就是小编给大家介绍的《Python之在函数中使用列表作为默认参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Golang 中函数作为值与类型
- golang-101-hacks(12)——切片作为函数参数传递
- Vue 中,如何将函数作为 props 传递给组件
- 在 C++ 中解包 std::vector 作为函数参数
- 将数组和矩阵传递给函数,作为C中指针的指针和指针
- React 组件模式-有状态组件 x 无状态组件、容器组件 x 展示组件、高阶组件 x 渲染回调(函数作为子组件)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming Collective Intelligence
Toby Segaran / O'Reilly Media / 2007-8-26 / USD 39.99
Want to tap the power behind search rankings, product recommendations, social bookmarking, and online matchmaking? This fascinating book demonstrates how you can build Web 2.0 applications to mine the......一起来看看 《Programming Collective Intelligence》 这本书的介绍吧!