可变参数指传入的参数个数是可变的,可以是0个,1个,2个...... 通过一个简单加法例子来了解一下
def sum(*nums):
type(nums)
ret = 0
for num in nums:
ret += num
print(ret)
sum(1, 2, 3, 4)
10
通过上面的例子我们可以看出,可变参数传入到函数内部时就变成了一个元组。当然直接传入列表也是可以实现可变参数的效果
def sum_list(num_list):
ret = 0
for num in num_list:
ret += num
print(ret)
num_list = [1, 2, 3, 4, 5]
sum_list(num_list)
15
在单独使用而不是在函数参数定义中使用*
会达到解包的效果,比如将一个列表传给可变参数
sum(*num_list)
15
关键字参数允许向函数传入个数可变的带有参数名的参数,可以是0个,1个,2个...... 通过一个例子了解一下
def student(stu_name, stu_age, stu_id, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'stu_id:', stu_id, 'other:', kw)
student('Tony', '22', '20180722', hometown='New York')
stu_name: Tony stu_age: 22 stu_id: 20180722 other: {'hometown': 'New York'}
函数内部,kw
已经变成了一个dict
类型了。同样,我们也可以给关键字参数传入一个dict
,但是同样需要解包,但是**kw
是拷贝到函数内部,在函数内部更改不会影响原先的dict
tony = {'hometown':'New York', 'phone':'11111111111'}
student('Tony', '22', '20180722', **tony)
stu_name: Tony stu_age: 22 stu_id: 20180722 other: {'hometown': 'New York', 'phone': '11111111111'}
当然,可以通过控制关键字参数的命名来限制调用者传入的参数,比如我们限制student()
允许调用者传入hometown
和phone
参数,但不允许其他的
def student(stu_name, stu_age, stu_id, *, hometown, phone):
print(stu_name, stu_age, stu_id, hometown, phone)
student('Tony', '22', '20180722', hometown='New York', phone='11111111111')
#student('Tony', '22', '20180722', 'New York', '11111111111') #报错
Tony 22 20180722 New York 11111111111
在定义命名的关键字参数时,需要使用*
将命名的关键字参数和位置参数分开。命名的关键字参数在调用时必须也把参数名也写上,不然会报错TypeError: student() takes 3 positional arguments but 5 were given
,意思也就是解释器把你传入的5个参数全部当成位置参数了,而我们的函数定义时只有3个位置参数,后面的两个是命名的关键字参数
python中定义函数时,有必须参数,默认参数,可变参数,关键字参数和命名关键字参数,5种参数除了可变参数和命名关键字参数外,其它的可以组合使用,但是要按照以上顺序定义参数
def student(stu_name, stu_age=20, *, stu_id, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'stu_id:', stu_id, 'kw:', kw)
student('Tony', stu_id='20170722', hometown='New York', phone='11111111111')
student('Tony', 23, stu_id='20170722', hometown='New York', phone='11111111111')
kw = {'hometown': 'A City', 'phone':'22222222222'}
student('Tony', 24, stu_id='20170722', **kw)
def student(stu_name, stu_age=20, *args, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'args:', args, 'kw:', kw)
student('Tony', 'a', 'b', 'c', hometown='New York', phone='33333333333')
student('Tony', 25, 'a', 'b', 'c', hometown='New York', phone='33333333333')
args = ('a', 'b', 'c')
kw = {'hometown':'New York', 'phone':'44444444444'}
student('Tony', *args, **kw)
student('Tony', 26, *args, **kw)
stu_name: Tony stu_age: 20 stu_id: 20170722 kw: {'hometown': 'New York', 'phone': '11111111111'}
stu_name: Tony stu_age: 23 stu_id: 20170722 kw: {'hometown': 'New York', 'phone': '11111111111'}
stu_name: Tony stu_age: 24 stu_id: 20170722 kw: {'hometown': 'A City', 'phone': '22222222222'}
stu_name: Tony stu_age: a args: ('b', 'c') kw: {'hometown': 'New York', 'phone': '33333333333'}
stu_name: Tony stu_age: 25 args: ('a', 'b', 'c') kw: {'hometown': 'New York', 'phone': '33333333333'}
stu_name: Tony stu_age: a args: ('b', 'c') kw: {'hometown': 'New York', 'phone': '44444444444'}
stu_name: Tony stu_age: 26 args: ('a', 'b', 'c') kw: {'hometown': 'New York', 'phone': '44444444444'}
网友评论