日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

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

傳遞任意數量的實參

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

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')

不管函數收到多少實參,這種語法都管用。

1. 結合使用位置實參和任意數量實參

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')

運行結果:

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

相關推薦:《Python視頻教程》

2. 使用任意數量的關鍵字實參

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中的兩個星號讓python創(chuàng)建了一個名為user_info的空字典。


網頁名稱:創(chuàng)新互聯Python教程:Python如何傳遞任意數量的實參
URL鏈接:http://www.5511xx.com/article/dpijsdh.html