Python 函式傳遞任意數量的實參

2021-09-27 12:38:43 字數 4801 閱讀 2388

# 案例 *toppings 形參名中的星號讓python建立了乙個空元組,並將收到的所有值都封裝到這個元組中

defmake_pizza

(*toppings)

:'''列印顧客點的所有配料'''

print

(toppings)

make_pizza(

'pepperoni'

)make_pizza(

'mushrooms'

,'green peopers'

,'extra cheese'

)

('pepperoni',)

('mushrooms', 'green peopers', 'extra cheese')

def

make_pizza

(*toppings)

:"""概述要做的比薩"""

print

("\n****** a pizza with the following toppings: "

)for topping in toppings:

print

("- "

+ topping)

make_pizza(

'pepperoni'

)make_pizza(

'mushrooms'

,'green peppers'

,'extra cheese'

)

****** a pizza with the following toppings: 

- pepperoni

****** a pizza with the following toppings:

- mushrooms

- green peppers

- extra cheese

def

make_pizza

(size,

*toppings)

:print

("\n****** a "

+str

(size)

+"-inch pizza with the following toppings:"

)for topping in toppings:

print

("- "

+ topping)

make_pizza(16,

'pepperoni'

)make_pizza(12,

'mushrooms'

,'green peppers'

,'extra cheese'

)

****** a 16-inch pizza with the following toppings:

- pepperoni

****** a 12-inch pizza with the following toppings:

- mushrooms

- green peppers

- extra cheese

# 案例

defbuild_profile

(first,last,

**user_info)

:'''建立乙個字典,其中包含我們知道的有關使用者的一切'''

profile =

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 =

'physics'

)print

(user_profile)

# 1.三明治

# 編寫乙個函式,它接受顧客要在三明治中新增的一系列食材。這個函式只有乙個形參(它收集函式呼叫中提供的所有食材),並列印一條訊息,對顧客

# 點的三明治進行概述。呼叫這個函式三次,每次都提供不同數量的實參

defadd_sandwich

(*foods)

:print

("\n i'll make you a great sandwich: "

)for food in foods:

print

("....adding "

+ food +

" to your sandwich."

)print

("your sandwich is ready!"

)add_sandwich(

'roast beef'

,'cheddar cheese'

,'lettuce'

,'honey dijon'

)add_sandwich(

'turkey',,

'honey mustard'

)add_sandwich(

'peanut butter'

,'strawberry jam'

)

i'll make you a great sandwich: 

....adding roast beef to your sandwich.

....adding cheddar cheese to your sandwich.

....adding lettuce to your sandwich.

....adding honey dijon to your sandwich.

your sandwich is ready!

i'll make you a great sandwich:

....adding turkey to your sandwich.

....adding honey mustard to your sandwich.

your sandwich is ready!

i'll make you a great sandwich:

....adding peanut butter to your sandwich.

....adding strawberry jam to your sandwich.

your sandwich is ready!

# 2.使用者簡介

# 在其中呼叫 build_profile() 來建立有關你的簡介;呼叫這個函式時,指定你的名和姓,以及三個描述你的鍵 - 值對。

defbuild_profile

(first,last,

**user_info)

:'''建立乙個字典,其中包含我們知道的有關使用者的一切'''

profile =

profile[

'first_name'

]= first

profile[

'last_name'

]= last

for key,value in user_info.items():

profile[key]

= value

return profile

user_profile = build_profile(

'li'

,'yege'

, height =

'170cm'

, weight =

'70kg'

, hobby =

'basketball',)

print

(user_profile)

# 3.汽車

# 編寫乙個函式,將一輛汽車的資訊儲存在乙個字典中。這個函式總是接受製造商和型號,還接受任意數量的關鍵字實參。這樣呼叫這個函式:提供必不可

# 少的資訊,以及兩個名稱 — 值對,如顏色和選裝配件。這個函式必須能夠像下面這樣進行呼叫

defbuild_cars

(made_address, model,

**user_info)

: cars =

cars[

'address'

]= made_address

cars[

'model'

]= model

for key,value in user_info.items():

cars[key]

= value

print

(cars)

build_cars(

'china'

,'big'

,color =

'blue'

, name =

'changcheng'

)

python 給函式傳遞任意數量的實參

1.在預先不知道有多少個實參的情況下。可以使用如下操作,允許函式從呼叫語句中收集任意數量的實參。def function name test print test function name 1 function name 1 2 3 輸出結果 1 1 2 3 形參 test讓python 建立乙個...

Python 向函式傳遞任意數量的實參

傳遞任意數量的實參 有時候,你預先不知道函式需要接受多少個實參,好在python允許函式從呼叫語句中收集任意數量的實參 def get letter letters for i in letters print i get letter a b c d e 形參名 letters中的星號讓pytho...

PHP傳遞任意數量的函式引數

下面這個示例向你展示了php函式的預設引數 兩個預設引數的函式 function foo arg1 arg2 foo hello world 輸出 arg1 hello arg2 world foo 輸出 arg1 arg2 下面這個示例是php的不定引數用法,其使用到了 func get args...