Python 函數(二)

用書 PYTHON王者歸來 作者洪錦魁

傳遞串列參數

def message(names):
    str1 = "Hi"
    str2 = "How are you?"
    str3 = "I am"
    for name in names:
        str = str1 +"\n" + str2 + "\n" +str3 + " " + name
        print(str,"\n") 

names = ["Kiki","Susu","Nini"]
message(names)

執行結果

傳遞一般變數與串列變數到函數

def data(n):
    print("id(n)=:",id(n),"\t",n)
    n = 5
    print("id(n)=:",id(n),"\t",n)

x=1
print("id(x)=:",id(x),"\t",x)
data(x)
print("id(x)=:",id(x),"\t",x)


def data1(n):
    print("id(n)=:",id(n),"\t",n)
    n[0] = 5
    print("id(n)=:",id(n),"\t",n)

y=[1,2]
print("id(y)=:",id(x),"\t",y)
data1(y)
print("id(y)=:",id(x),"\t",y)

執行結果

函數的修改串列的內容(預約產品手續)

def product(unreservation,reservation):
    print(">>>>>>預約手續進行中>>>>>>")
    while unreservation:
        current = unreservation.pop()
        print("預約產品>>>>>>",current)
        reservation.append(current)

def show_unreservation(unreservation):
    print("✖ 還沒預約的產品")
    if not unreservation:
        print("目前沒有還沒預約的產品")
    for unreservation_products in unreservation:
        print(unreservation_products)

def show_reservation(reservation):
    print("◎ 已經預約的產品")
    if not reservation:
        print("目前沒已經預約的產品")
    for resevation_products in reservation:
        print(resevation_products)

unreservation=["麥片","年糕","棒棒糖"]
reservation=["泡麵"]

show_unreservation(unreservation)
show_reservation(reservation)

執行結果

以下的部分重新改串列名也不會影響結果

函數與主程式的變數名不一定要相同

原因是 因為函數串列 unreservation 和 reservation 與主程式串列unreservation_list=[ ] reservation_list=[ ]是相同的記憶體位置。函數更改串列內容時,主程式串列也隨著更改

傳遞副本串列( 傳遞串列的切片拷貝)

串列的切片拷貝 ID不同

上述的unreservation_list已經變成空串列了,若是想保存預約內產品內容的話,可再呼叫product()函數時傳遞副本串列 product(unreservation_list[:],reservation_list)

def product(unreservation,reservation):
    print(">>>>>>預約手續進行中>>>>>>")
    while unreservation:
        current = unreservation.pop()
        print("預約產品>>>>>>",current)
        reservation.append(current)

def show_unreservation(unreservation):
    print("✖ 還沒預約的產品")
    if not unreservation:
        print("目前沒有還沒預約的產品")
    for unreservation_products in unreservation:
        print(unreservation_products)

def show_reservation(reservation):
    print("◎ 已經預約的產品")
    if not reservation:
        print("目前沒已經預約的產品")
    for resevation_products in reservation:
        print(resevation_products)

unreservation_list=["麥片","年糕","棒棒糖"]
reservation_list=["泡麵"]

show_unreservation(unreservation_list)
show_reservation(reservation_list)

product(unreservation_list[:],reservation_list)
print("---------over----------")

show_unreservation(unreservation_list)
show_reservation(reservation_list)

product(unreservation_list[:],reservation_list)  這邊傳的是副本串列不是空集合]

執行結果

這是剛剛的結果(變成空串列的)

串列會遺留先前呼叫的內容

def insertletter(anyletter,null_list=[],number_list=[1,2]):
    null_list.append(anyletter)
    number_list.append(anyletter)
    print(null_list)
    print(number_list)

insertletter("nini")
insertletter("f")

執行結果

不讓串列呼叫先前內容

def insertletter(anyletter,null_list=None):
    if null_list == None:
        null_list=[]
    null_list.append(anyletter)
    print(null_list)
   
insertletter("nini")
insertletter("f")

執行結果

傳遞處理任意數量參數

drink(*alltea) 是元組

def drink(*alltea):
    print("----tea meau----")
    for tea in alltea:
        print(tea)
    print(type(alltea))
    print(alltea)
    
drink("green tea")
print("...........................")
drink("lemon tea","black tea","milk tea")
print("...........................")

執行結果

沒傳入參數

def drink(*alltea):
    print("----tea meau----")
    for tea in alltea:
        print(tea)
    

drink()

執行結果

一般參數和任意數量參數的組合

def drink(cake,*alltea):
    print("Oder this cake:", cake ,",you can select ----tea meau----")
    for tea in alltea:
        print(tea)

drink("chocolate cake","green tea")
print("...........................")
drink("lemon cake","lemon tea","black tea","milk tea")
print("...........................")def drink(cake,*alltea):
    print("Oder this cake:", cake ,",you can select ----tea meau----")
    for tea in alltea:
        print(tea)

drink("chocolate cake","green tea")
print("...........................")
drink("lemon cake","lemon tea","black tea","milk tea")
print("...........................")

執行結果

一般參數和任意數量參數的組合 (字典)

**info 可以自行命名的參數

可以變成任何數量的字典元素

def classmate_info(name,number,**info):
    info_dict={}
    info_dict["name"]= name
    info_dict["number"]=number
    for key,value in info.items():
        info_dict[key]=value
    return info_dict
    
calssmate = classmate_info("Bily","1",
                           English= 100,
                           Math= 100)
print(calssmate)

執行結果