文章目录
形参的分类
必需的参数
形参与实参一一对应的关系,个数对应,位置对应。
个数不一致报错(会报:missing 1 required positional argument: ‘jb’)
位置不一致会导致错误输出,也有可能报错(数据类型得一致)
举例
def profile(name,age,company,department,position,salary,jb):
outputmsg = '''
***********************Personal Information for %s***************************
* Your Name : %s
* Your Age : %s
* Your Company : %s
* Your Department : %s
* Your position : %s
* Your Salary : %s
* Your Job description : %s
*
****************************************************************************
''' % (name, name, age, company, department, position, salary, jb)
print(outputmsg)
#########################################################################################
profile("Elon Mask",30,"Tesla","Senior Management","CEO","300000000$","govern")
###########以下为输出的内容#############
"""
***********************Personal Information for Elon Mask***************************
* Your Name : Elon Mask
* Your Age : 30
* Your Company : Tesla
* Your Department : Senior Management
* Your position : CEO
* Your Salary : 300000000$
* Your Job description : govern
*
****************************************************************************
"""
关键字参数
在调用函数时,使用形参中定义的关键字来指定哪个形参配置什么实参的参数。
指定关键字,则允许函数调用参数时的顺序不一致,但是个数必需得一致。
实验:关键字参数
上述实验中只需要修改调用函数的参数即可,如下:
profile(age=30,name="Elon Mask",company="Tesla",position="CEO",department="Senior Management",salary="300000000$",jb="govern")
默认参数
- 调用函数时,某个形参没有传入相应的实参的值,则被认为是默认值。下面例子,company没有传参就默认为“Tesla”
- 默认参数只能放在最后
- 默认参数可以有多个
正常示例
def profile(name,age,department,position,salary,jb,company='Tesla'):
print("name:%s"%name)
print("age:%s"%age)
print("company:%s"%company)
print("department:%s"%department)
print("position:%s"%position)
print("salary:%s"%salary)
print("jobdescription:%s"%jb)
profile("Elon Mask",30,"Senior Management","CEO","300000000$","govern")
###########以下为输出的内容#############
"""
name:Elon Mask
age:30
company:Tesla
department:Senior Management
position:CEO
salary:300000000$
jobdescription:govern
"""
报错示例(有默认值的参数不在最后)
def profile(name,age,department,position,salary,company='Tesla',jb):
print("name:%s"%name)
print("age:%s"%age)
print("company:%s"%company)
print("department:%s"%department)
print("position:%s"%position)
print("salary:%s"%salary)
print("jobdescription:%s"%jb)
profile("Elon Mask",30,"Senior Management","CEO","300000000$","govern")
###########以下为输出的内容#############
"""
def profile(name,age,department,position,salary,company='Tesla',jb):
^
SyntaxError: non-default argument follows default argument
"""
正常示例(多个默认值参数)
def profile(name,age,department,position,salary,company='Tesla',jb="Employer"):
print("name:%s"%name)
print("age:%s"%age)
print("company:%s"%company)
print("department:%s"%department)
print("position:%s"%position)
print("salary:%s"%salary)
print("jobdescription:%s"%jb)
profile("Elon Mask",30,"Senior Management","CEO","300000000$")
###########以下为输出的内容#############
"""
name:Elon Mask
age:30
company:Tesla
department:Senior Management
position:CEO
salary:300000000$
jobdescription:Employer
"""
不定长参数
有一类函数,在定义时无法确定在调用时会传入多少个实参,在这种情况下使用一种特殊的标记:
- *(一个星号):存放所有的未命名的参数,全部存放在这一个变量中
- 输入的数据类型为元组
- **(两个星号):存放命名的参数
- 输入的数据类型为字典
实验一:* 未命名的不定长参数
def numberadd(*args):
print(type(args)) # 用于调用此函数时,这种未命名的参数的数据类型
numbersum = 0
for i in args:
numbersum += i
print(numbersum)
numberadd(1,2,3,4,5,6,7,8,9,10)
numberadd(1,2,3,4,6,7,8,9,10)
"""
<class 'tuple'>
55
<class 'tuple'>
50
"""
实验一:** 命名的不定长参数
def profile(**args):
print(type(args))
print(args)
print("分割符".center(50,"*"))
for i in args:
print("%s:%s"%(i,args[i]))
profile(name="Elon Musk",age="35",company="Tesla",)
"""
<class 'dict'>
{'name': 'Elon Musk', 'age': '35', 'company': 'Tesla'}
***********************分割符************************
name:Elon Musk
age:35
company:Tesla
"""
实验三:另一种传参数的方式
def function(*args):
print(args)
function(*(1,2,3,4,5))
def functions(**Test):
print(Test)
functions(**{"name":"Elon Musk","Age":"35"})
"""
(1, 2, 3, 4, 5)
{'name': 'Elon Musk', 'Age': '35'}
"""