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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章