
print("""
There is only one type of parameter passing in Python, which is object reference passing.
In the function body, mutable and immutable objects have different behaviours.
""")
def fl(l):
l.append(1)
print(l)
def fs(s):
s += 'a'
print(s)
ll = []
fl(ll)
fl(ll)
ss = "hehe"
fs(ss)
fs(ss)
print("#####################")
print("""
a test
""")
def clear_list(l):
l = []
ll = [1, 2, 3]
clear_list(ll)
print(ll)
print("""
default parameter's side effect.
default parameters take effect only once.
""")
def flist(l=[1], ll=[2]):
l.append(1)
ll.append(2)
print(l)
print(ll)
flist()
flist()
output
There is only one type of parameter passing in Python, which is object reference passing.
In the function body, mutable and immutable objects have different behaviours.
[1]
[1, 1]
hehea
hehea
#####################
a test
[1, 2, 3]
default parameter's side effect.
default parameter take effect once.
[1, 1]
[2, 2]
[1, 1, 1]
[2, 2, 2]
本文深入探讨了Python中唯一的一种参数传递方式:对象引用传递。详细分析了在函数体内部,可变与不可变对象的不同行为,包括列表和字符串的操作实例。同时,揭示了默认参数的副作用,展示了如何避免常见的陷阱。
2693

被折叠的 条评论
为什么被折叠?



