PYTHON基础

11.函数传参

2020-12-19  本文已影响0人  Stone_説

目录:
1.位置参数
2.关键字参数
3.参数默认值
4.可变参数
5.keyword-only参数
6.可变参数和参数默认值
7.参数解构

1.位置参数

参数调用时传入的参数要和定义的个数相匹配

>>> def fn(x,y,z):
...     print(x)
...     print(y)
...     print(z)
>>> fn(2,4,5)
2
4
5
>>> fn(2,4,55,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() takes 3 positional arguments but 4 were given
>>> fn(2,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() missing 1 required positional argument: 'z'

2.关键字参数

使用形参的名字来传入实参的方式,则传入的参数的顺序可以和定义顺序不同
要求:位置参数必须在关键字参数之前传入

>>> def fn(x,y,z):
...     print(x,y,z)
... 
>>> fn(x=9,z=3,y=10)
9 10 3
>>> fn(x=9,y=3,10)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument

3.参数默认值

参数的默认值可以在未传入足够的实参的时候,对没有给定的参数赋值为默认值
参数非常多的时候,并不需要用户每次都输入所有的参数,简化函数调用

>>> def fn(x=1,y=2):
...     print(x,y)
>>> fn()
1 2
>>> fn(4,5)
4 5
>>> fn(y=5)
1 5

4.可变参数

一个形参可以匹配任意个参数

4.1位置参数的可变参数

在形参前使用*表示该形参是可变参数,可以接收多个实参
收集多个实参为一个tuple

>>> def add(*nums):
...     sum = 0
...     print(type(nums))
...     for x in nums:
...             sum += x
...     print(sum)
... 
>>> add(1,2,3)
<class 'tuple'>
6
>>> add(*(1,2,3))
<class 'tuple'>
6
>>> add((1,2,3))
<class 'tuple'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in add
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
4.2关键字参数的可变参数

形参前使用**符号,表示可以接收多个关键字参数
收集的实参名称和值组成一个字典

>>> def showconfig(**kwargs):
...     for k,v in kwargs.items():
...             print(k,v)
>>> showconfig(a=1,b=2)
a 1
b 2
4.3可变参数总结

可变参数的混合使用:

def showconfig(username,password,**kwargs)
def showconfig(username,*args,**kwargs)
def showconfig(username,password,**kwargs,*args)

可变参数总结:
有位置可变参数和关键字可变参数
位置可变参数在形参前使用一个星号*
关键字可变参数在形参前使用两个星号
位置可变参数和关键字可变参数都可以收集若干个实参,位置可变参数收集形成一个tuple,关键字可变参数收集形成一个dict
混合使用参数的时候,可变参数要放到参数列表的最后,普通参数需要放到参数列表前面,位置可变参数需要在关键字可变参数之前

5.keyword-only参数

keyword-only参数:如果在一个星号参数后,或者一个位置可变参数后,出现的普通参数,实际上已经不是普通的参数了,而是keyword-only参数

>>> def fn(*args,x):
...     print(x),print(args)
>>> fn(3,5,7,x=100)
100
(3, 5, 7)
>>> fn(3,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() missing 1 required keyword-only argument: 'x'
>>> def fn(**kwargs,x):
  File "<stdin>", line 1
    def fn(**kwargs,x):
                    ^
SyntaxError: invalid syntax

直接语法报错,kv的数据会直接被kwargs截取,存入对应dict

keyword-only参数,另一种形式:

>>> def fn(*,x,y):
...     print(x,y)
>>> fn(x=29,y=6)
29 6
>>> fn(2,4,5,6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() takes 0 positional arguments but 4 were given
>>> fn(2,4,x=29,y=6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() takes 0 positional arguments but 2 positional arguments (and 2 keyword-only arguments) were given
>>> fn(x=29,y=6,2,4,5)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument

*号之后,普通形参都变成了必须给出的keyword-only参数

6.可变参数和参数默认值

>>> def fn(*args,x=5):
...     print(x)
...     print(args)
... 
>>> fn()
5
()
>>> fn(5)
5
(5,)
>>> fn(x=7)
7
()
>>> fn(1,2,3,x=10)
10
(1, 2, 3)
>>> def fn(y,*args,x=5):
...     print(x,y)
...     print(args)
... 
>>> fn()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() missing 1 required positional argument: 'y'
>>> fn(5)
5 5
()
>>> fn(1,2,3,x=100)
100 1
(2, 3)
>>> fn(y=99,2,3,x=100)
  File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> fn(2,3,y=99,x=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fn() got multiple values for argument 'y'
>>> def fn(x=5,**kwargs):
...     print(x)
...     print(kwargs,type(kwargs))
... 
>>> fn()
5
{} <class 'dict'>
>>> fn(55)
55
{} <class 'dict'>
>>> fn(x=100)
100
{} <class 'dict'>
>>> fn(y=99,x=100)
100
{'y': 99} <class 'dict'>
>>> fn(88,y=99)
88
{'y': 99} <class 'dict'>

参数规则举例:
参数列表参数一般顺序是,普通参数,缺省参数,可变位置参数,keyword-only参数(带缺省),可变关键字参数

7.参数解构

给参数提供实参的时候,可以在集合类型前使用*或者**,把集合类型的解构解开,提取出所有元素作为函数的实参
非字典类型使用*结构成位置参数
字典类型使用**解构成关键字参数
提取出来的元素数目要和参数的要求匹配,也要和参数的类型匹配

>>> def add(*iterable):
...     result = 0
...     for x in iterable:
...             result += x
...     print(result),print(iterable,type(iterable))
... 
>>> add(1,4,5)
10
(1, 4, 5) <class 'tuple'>
>>> add(*[1,3,5])
9
(1, 3, 5) <class 'tuple'>
>>> add(*(1,2,3))
6
(1, 2, 3) <class 'tuple'>
>>> add(*range(4))
6
(0, 1, 2, 3) <class 'tuple'>
>>> add(**range(4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add() argument after ** must be a mapping, not range
上一篇 下一篇

猜你喜欢

热点阅读