Python学习笔记四_函数参数、函数传值方式、传参注意点

缺省参数

定义:函数在定义的时候参数就有值了,这样的参数叫缺省参数(默认值)

举例说明:

def num_sum(num1,num2):
    result = num1 + num2
    return result

value1 = num_sum(1,2)
value2 = num_sum(2,2)
value3 = num_sum(3,2)
print(value1,value2,value3)

其中 value1,value2,value3中,num2实际传的参数都是一样的,由此可以精简

def num_sum(num1,num2=2):
    result = num1 + num2
    return result

value1 = num_sum(1)   #num2不写,参数为函数中显示的默认值
value2 = num_sum(2)
value3 = num_sum(3,30)  #如果传了实际参数,就按照实际的参数进行计算
print(value1,value2,value3)

输出结果:

3 4 33
Process finished with exit code 0

如果函数的第一个参数设置成了缺省参数,后面都得设置成缺省参数

举例说明:(会报错)

def num_sum(num1 = 1 ,sep):  #正确的写成 sep = 2
	result = num1 + sep
	return result
value = num_sum(1,3)
print(value)

函数传值的方式

1. 位置传参(必须是一一对应,必选参数)

举例说明:

def test(name, age):
    print(name, age)

test('woho',3)
test(3,'woho')

输出结果:

woho 3
3 woho
Process finished with exit code 0

2. 关键字传参(避免顺序出错)

举例说明:

def test(name, age):
    print(name, age)

test(name='woho',age=3)
test(age=3,name='woho')

输出结果:

woho 3
woho 3
Process finished with exit code 0

注意点:如果前面使用关键字传参,后面也必须使用关键字传参
举例说明:

def test(name, age):
    print(name, age)

# test('woho', age=3) # 错误的
# test(name='woho', 3) # 错误的
test(name ='woho', age=3) #正确的

输出结果:

woho 3
Process finished with exit code 0

3. 不定长参数传参

不定长形式参数:(两种+补充)

  • 不定长位置形式参数

格式:* 加上变量名

举例说明:

def num_sum(*args):

	print(args,type(args)) #   class 'tuple'
	
	result = 0   #  输入的数字求和
	for value in args:
		result = result + value
	return result

a = num_sum(1,2,3,4,5)
print(a)

其中* args不定长位置参数,以集合形式输出, args只是一个变量名字,可以改的, *必须要有

输出结果:

(1, 2, 3, 4, 5) <class ‘tuple’>
15
Process finished with exit code 0

注意点:不定长位置形式参数是不接收 关键字参数传参

举例说明:(会报错)

def num_sum(*args):
	print(args,type(args))

num_sum(a =1 ,b = 2)  #运行报错
  • 不定长关键字形式参数

格式:**+ 加上变量名

举例说明:

def show(**kwargs):

	print(kwargs,type(kwargs))  #dict

	for key,value in kwargs.items():
		print(key,value)

show(a = 1,b = 2)

其中**kwargs 会接收,以关键字传过来的参数 ,并以字典形式输出,kwargs只是个变量的名字也可以改的

输出结果:

{‘a’: 1, ‘b’: 2} <class ‘dict’>
a 1
b 2
Process finished with exit code 0参数

注意点:不定长关键字形式参数只接收关键字参数传参,不接受位置

举例说明:(会报错)

def show(**kwargs):
	print(kwargs,type(kwargs))  
    	
show(1,2)

传参的注意点

传参需要注意参数的位置顺序

  1. 位置参数,不定长位置参数,不定长关键字参数

举例说明:

def show(name,age,*args,**kwargs):
	print(name,age,args,kwargs)

show('woho',3,6,9,88,a=1,b=2)
# show(name='woho',age=3,1,2)  # 会报错

输出结果:

woho 3 (6, 9, 88) {‘a’: 1, ‘b’: 2}
Process finished with exit code 0

可以看出,woho 3 对应的是name 和 age 的位置参数,(6,9,88)对应的是*args 不定长位置参数,{‘a’: 1, ‘b’: 2} 对应**args不定长关键字参数,参数顺序变化是会报错的

  1. 不定长位置参数,位置参数,不定长关键字参数

举例说明:

def show(*args,name,age,**kwargs):
	print(name,age,args,kwargs)

#show(1,2,'woho',3,a=1,b=2) #会报错,系统认为参数都给了*args,系统认为缺失name 和age 关键字参数
show(1,2,name='woho',age=3,a=1,b=2)  
show(1,2,name='woho',age=3)    #不定长关键字参数认为是空值
show(name='woho',age=3,a=1,b=2)   #不定长位置参数认为是空值

输出结果:

woho 3 (1, 2) {‘a’: 1, ‘b’: 2}
woho 3 (1, 2) {}
woho 3 () {‘a’: 1, ‘b’: 2}
Process finished with exit code 0

  1. 不定长的关键字参数**kwargs ,必须放在所有形式参数的最后

举例说明:(会报错)

def show(**args,name,age,*kwargs):
	print(name,age,args,kwargs)
  1. 当形式参数里出现缺省参数

举例说明:

def show(name,age=18,*args,**kwargs):
	print(name,age,args,kwargs)

show('woho',20,'woho1',a=1)
show('woho','woho1',a=1)  #不定长位置参数会变成空值

输出结果:

woho 20 (‘woho1’,) {‘a’: 1}
woho woho1 () {‘a’: 1}
Process finished with exit code 0

如果缺省参数放在第二位,没有 age 参数时,woho 和 woho1都变成了位置参数,需要调换缺省参数第二位和第三位位置可以避免这种情况

举例说明:

def show(name,*args,age=18,**kwargs):
	print(name,age,args,kwargs)

show('woho','woho1',a=1)
show('woho','woho1',20,a=1)
show('woho','woho1',age=20,a=1)

输出结果:

woho 18 (‘woho1’,) {‘a’: 1}
woho 18 (‘woho1’, 20) {‘a’: 1}
woho 20 (‘woho1’,) {‘a’: 1}
Process finished with exit code 0

定义函数参数的时候,复杂情况也就以下情况
格式:
def funct(name,age,*args,**kwargs)
def funct(*args,**kwargs):

  1. 让函数传参必须使用关键字传参

格式:* 加参数

*的右边必须使用关键字形式

举例说明:

def show(*,name,age):
	print(name,age)

show(name='woho',age=18)
# show('woho',18) # 会报错

# * 后面的参数,必须用关键字形势
def show(sex,*, name, age):
	print(sex,name, age)

show('woman',name='woho', age=3)
show(sex='woman',name='woho', age=3)

输出结果:

woman woho 3
woman woho 3
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_43817580/article/details/86749743
今日推荐