parameter passing

在这里插入图片描述


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]
发布了83 篇原创文章 · 获赞 4 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/hope_worker/article/details/104246795