• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
QQ糖_尤尤
博客园    首页    新随笔    联系   管理    订阅  订阅
8.5 传递任意数量的实参

有时候预先不知道函数需要接受多少个实参,python允许函数从调用语句中收集任意数量的实参。

"""
这是一个制作比萨的函数,它需要接受很多配料,但无法预先确定顾客要多少种配料
函数中形参名*toppings,"*"功能是创建一个名为toppings的空元组,并将收的的所有值都封装到这个元组中
"""
def make_pizza(*toppings):

    """打印顾客点的所有配料"""
    print(toppings)

make_pizza("peperoni")
make_pizza("mushrooms","green peppers","extra cheese")



运行结果:
>>> 
======================== RESTART: D:/python学习/8.5.py ========================
('peperoni',)
('mushrooms', 'green peppers', 'extra cheese')
>>> 

 现在我们可以用循环语句,对配料表进行遍历,并对顾客点的比萨进行描述:

"""
这是一个制作比萨的函数,它需要接受很多配料,但无法预先确定顾客要多少种配料
函数中形参名*toppings,"*"功能是创建一个名为toppings的空元组,并将收的的所有值都封装到这个元组中
"""
def make_pizza(*toppings):

    """打印顾客点的所有配料"""
    print("\nMakeing a pizza with the following toppings:")
    for topping in toppings:
        print("-"+topping)

make_pizza("peperoni")
make_pizza("mushrooms","green peppers","extra cheese")


运行结果:
>>> 
======================== RESTART: D:/python学习/8.5.py ========================

Makeing a pizza with the following toppings:
-peperoni

Makeing a pizza with the following toppings:
-mushrooms
-green peppers
-extra cheese
>>> 

 8.5.1 结合使用位置实参和任意数量实参

如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量 实参的形参放在最后。python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。

例如:前面的函数还需要加一个表示比萨尺寸的实参 ,必须将该形参放在形参*toppings的前面。

"""
这是一个制作比萨的函数,它需要接受很多配料,但无法预先确定顾客要多少种配料
函数中形参名*toppings,"*"功能是创建一个名为toppings的空元组,并将收的的所有值都封装到这个元组中
"""
def make_pizza(size,*toppings):

    """概述要制作的比萨"""
    print("\nMakeing a "+str(size)+"-inch pizza with the following toppings:")
    for topping in toppings:
        print("-"+topping)

make_pizza(16,"peperoni")
make_pizza(12,"mushrooms","green peppers","extra cheese")


运行结果:

>>> 
======================== RESTART: D:/python学习/8.5.py ========================

Makeing a 16-inch pizza with the following toppings:
-peperoni

Makeing a 12-inch pizza with the following toppings:
-mushrooms
-green peppers
-extra cheese
>>> 

 8.5.2 使用任意数量的关键字实参

有时候需要接受任意数量任意类型的信息,可将函数编写成能够接受任意数量的键-值对。

"""函数 build_profile()的定义要求提供名和姓,同时允许用户根据需要提供任意数量的名称-值对。
   形参**user_info,两个*的功能是创建一个名为user_info的空字典,并将收集到的所有名称-对都封装到这个字典中。
   
在该函数中可以像访问其他字典那样访问user_info中的名称-值对。
"""


def build_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)


运行结果:
>>> 
================== RESTART: C:/Users/admin/Desktop/8.5.2.py ==================
{'first_name': 'albert', 'last_name': 'einstein', 'field': 'physics', 'location': 'princeton'}
>>> 
说明:返回的字典包含用户的名、姓,还有求学的地方和所学专业。调用这个函数不管额外提供了多少个键-值对都能正确处理

 

posted on 2021-01-18 13:00  QQ糖_尤尤  阅读(97)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3