日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python如何傳遞任意數(shù)量的實(shí)參

傳遞任意數(shù)量的實(shí)參

形參前加一個(gè) * ,python會(huì)創(chuàng)建一個(gè)已形參為名的空元組,將所有收到的值都放到這個(gè)元組中: 

def make_pizza(*toppings):
    print("\nMaking a pizza with the following toppings: ")
    for topping in toppings:
        print("- " + topping)
make_pizza('pepperoni')
make_pizza('mushroom', 'green peppers', 'extra cheese')

不管函數(shù)收到多少實(shí)參,這種語法都管用。

1. 結(jié)合使用位置實(shí)參和任意數(shù)量實(shí)參

def make_pizza(size, *toppings):
    print("\nMaking a " + str(size) + "-inch pizza with the following toppings: ")
    for topping in toppings:
        print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushroom', 'green peppers', 'extra cheese')

運(yùn)行結(jié)果:

Making a 16-inch pizza with the following toppings: 
- pepperoni
Making a 12-inch pizza with the following toppings: 
- mushroom
- green peppers
- extra cheese

相關(guān)推薦:《Python視頻教程》

2. 使用任意數(shù)量的關(guān)鍵字實(shí)參

def build_profile(first, last, **user_info):
    profile = dict()
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
        profile[key] = value
    return profile
user_profile = build_profile('albert', 'einstein', location='princeton', field='physic')
print(user_profile)

形參**user_info中的兩個(gè)星號讓python創(chuàng)建了一個(gè)名為user_info的空字典。


分享名稱:創(chuàng)新互聯(lián)Python教程:Python如何傳遞任意數(shù)量的實(shí)參
文章路徑:http://www.dlmjj.cn/article/dpijsdh.html