python入门_day5_Chap8_函数后续

8.5传递任意数量的函数

1.形参*toppings

def make_pizza(*toppings):
    print(toppings)

make_pizza('peper')
make_pizza('mushroom','green peppers','extra cheese')

形参名*toppings 中的星号让Python创建一个名为toppings 的空元组,并将收到的所有值都封装到这个元组中。函数体内的print 语句通过生成输出来证明Python能够处理 使用一个值调用函数的情形,也能处理使用三个值来调用函数的情形。

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

def make_pizza(size,*toppings):
    print('\n Making a '+str(size)+'-inch pizza with the toppings:')

    for topping in toppings:
        print('-'+topping)

make_pizza(12,'peper')

make_pizza(8,'mushroom','green peppers','extra cheese')

运行结果:
在这里插入图片描述

3.使用任意数量的关键字实参
函数build_profile() 的定义要求提供名和姓,同时允许用户根据需要提供任意数量的名称—值对
形参user_info 中的两个星号让Python创建一个名为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)

练习
1.三明治 :编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客 点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。

def make_sandwichs(*toppings):
    print('Add '+str(toppings)+' to your sandwich')

make_sandwichs('cabage','beef','egg')
make_sandwichs('beef','egg')
make_sandwichs('cabage','egg')

运行结果:
在这里插入图片描述
2.3 用户简介 :复制前面的程序user_profile.py,在其中调用build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值 对。

def user_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

my_profile=user_profile('Ming','jing',location='hubei',age=20,identity='student')
print(my_profile)

运行结果:
在这里插入图片描述

3.汽车 :编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可 少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:

def make_car(producer,car_id,**car_info):
    car={}
    car['producer']=producer
    car['car_id']=car_id
    for key,value in car_info.items():
        car[key]=value

    return car

car=make_car('subarru','outback',color='blue',tow_package=True)
print(car)
8.6将函数储在模块中

函数的优点之一是,使用它们可将代码块与主程序分离。
通过给函数指定描述性名称,可让主程序容易理解得多。

你还可以更进一步,将函数存储在被称为模块 模块 的独立文件中, 再将模块导入 导入 到主程序中。

import 语句允许在当前运行的程序文件中使用模块中的代码。

1.导入整个模块

创建pizza.py

def make_pizza(size,*toppings):
    print('\nMakeing a '+str(size)+'-inch pizza with the following toppings:')
    for topping in toppings:
        print('-'+topping)

创建making-pizza.py的文件,使用import语句,调用两次make_pizza()
函数

import pizza

pizza.make_pizza(16,'pepperoni')
pizza.make_pizza(12,'mushrooms','green peppers','extra cheese')

运行结果:
在这里插入图片描述

2.导入特定的函数

语法:
from module_name import function_name

tip:
导入多个函数时,使用逗号分隔。
from module_name import function_0, function_1, function_2

披萨的例子:

from pizza import make_pizza

pizza.make_pizza(16,'pepperoni')
pizza.make_pizza(12,'mushrooms','green peppers','extra cheese')

3.使用 as 给函数指定其他名字

使用关键字 as 将meke_pizza重新命名为mp

from pizza import make_pizza as mp

mp(16,'pepperoni')
mp(12,'mushrooms','green peppers','extra cheese')

运行结果和上面相同

4.导入模块中的所有函数

使用星号(* )运算符可让Python导入模块中的所有函数

from pizza import *

make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')

Sum:(Best policy)
最佳的做法是,要么只导入你需要使用的函数,要么导入整个模块并使用句点表示法

编写函数的注意事项
1.给形参指定默认值时,等号两边不要有空格。

2.大多数编辑器都会自动对齐后续参数列表行,使其缩进程度与你给第一个参数列表行指定的缩进程度相同:
在这里插入图片描述

3.如果程序或模块包含多个函数,可使用两个空行将相邻的函数分开

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值