Python 官方文档:入门教程 => 点击学习
定义一个方法,接收一个 id参数和一个 list 参数 def test(id, ids=[]): if id: ids.append(id) print ids test(id=1) test(
def test(id, ids=[]):
if id:
ids.append(id)
print ids
test(id=1)
test(id=2)
[1]
[1, 2]
上面方法的定义方式跟下面是一样的,相当于是参数引用的是一个全局变量,所以它的值一直在累加
ids = []
def test(id, ids):
if id:
ids.append(id)
print ids
test(id=1)
test(id=2)
def test(id, ids=None):
if id:
ids = list()
ids.append(id)
print ids
test(id=1)
test(id=2)
--结束END--
本文标题: python list 参数传递方式
本文链接: https://www.lsjlt.com/news/189432.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0