向python的函数中传参数的几种方法

参考链接:http://www.cnblogs.com/rourou1/p/6182502.html


python中函数根据是否有返回值可以分为四种:

  • 无参数无返回值
  • 无参数有返回值
  • 有参数无返回值
  • 有参数有返回值

Python中传递参数的形式主要有下面几种:

  1. 位置传递
  2. 关键字传递
  3. 参数默认值
  4. 包裹传递
  5. 解包裹

下面逐个简单说明下:

位置传递

为止传递,很明显,就是实参对应形参的为止将参数传递进去,这样传递的时候,需要按照形参的顺序传入对应的值。举个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> def fun(a,b,c):
... return a+b+c
...
>>> print(fun(1,2,3))
6
>>> def fun(a,b,c):
... print("a = "+ a)
... print("b = "+ b)
... print("c = "+ c)
... return a+b+c
...
>>> print(fun(1,2,3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fun
TypeError: Can't convert 'int' object to str implicitly
>>> def fun(a,b,c):
... print("a = "+ a)
... print("b = "+ b)
... print("c = "+ c)
... return a+b+c
>>> print(fun('1','2','3'))
a = 1
b = 2
c = 3
123

这里注意传进函数的参数的类型,第一次传入的类型是整型数类型,“+”在这里是加法的运算符号,但是使用print()函数的时候,里面的参数必须是字符串,没有转换的时候,直接print()会出错。

关键字传递

关键字传递是根据每个形参的名字进行实参的传递,关键字传递的时候可以不用遵循位置的关系。

1
2
3
4
5
6
7
8
9
>>> print(fun('1',a='2', b='3'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: fun() got multiple values for argument 'a'
>>> print(fun(c='1',a='2', b='3'))
a = 2
b = 3
c = 1
231

按照关键字传递和按照位置传递参数可以混用,但是混用的时候,没有关键字的就按照位置传递,后面在使用关键字传递一次就会发生冲突。

参数默认值

有的参数在声明的时候会给参数传递一个默认值,如果后面调用这个函数的时候,没有给这个参数传递进新的值,就会保留这个默认值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>>> def fun(a,b,c=10):
... print("a="+str(a))
... print("b="+str(b))
... print("c="+str(c))
... return a+b+c
...
# 没有给c赋值的时候,我们使用c的默认值
>>> print(fun(1,2))
a=1
b=2
c=10
13
# 给c传递新的参数的时候,使用新的传入的参数
>>> print(fun(1,2,21))
a=1
b=2
c=21
24
# 一个函数的参数是固定的,多传和少传都会报错
>>> print(fun(1,2,c=21,c=19))
File "<stdin>", line 1
SyntaxError: keyword argument repeated

如果重复传递一个参数会出错。

包裹传递

有的时候在定义函数的时候,我们有时候并不知道调用的时候换传递多少个参数,这时候包裹位置参数或者包裹关键字参数,来进行参数的传递,是一个有效的方法。

1
2
3
4
5
6
7
8
9
10
>>> def func(*name):
... print(type(name))
... print(str(name))
...
>>> func(1,2,3)
<class 'tuple'>
(1, 2, 3)
>>> func(1,2,3,4,5,6,7,8)
<class 'tuple'>
(1, 2, 3, 4, 5, 6, 7, 8)

在这里我们先定义了函数func(),但是我们不知道要传递多少参数进入这个函数,于是我们使用name收集所有的参数,这样就根据位置合并成一个元组(tuple),这叫做包裹为止传递。

我们在定义func()的时候,在name前面加了*,是告诉python参数,name是包裹位置传递所用的元组名。

那么怎样包裹关键字传递参数呢?我们使用关键字传递参数的时候,类似于字典中的键和值。所以尝试使用字典包裹传递:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>>> mydict = dict(a=2,b=3,c=4)
>>> mydict
{'b': 3, 'c': 4, 'a': 2}
>>> type(mydict)
<class 'dict'>
>>> def func(**dict):
... print(type(dict))
... print(dict)
...
>>> func(1,9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 0 positional arguments but 2 were given
>>> func(mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 0 positional arguments but 1 was given
>>> func(**mydict)
<class 'dict'>
{'b': 3, 'c': 4, 'a': 2}
# 直接往函数中传递一个字典包裹关键字传递的参数也可以
>>> func(**{"name":"Python", "age":"24", "Address":"Beijing"})
<class 'dict'>
{'name': 'Python', 'Address': 'Beijing', 'age': '24'}

我们使用字典包裹关键字传递参数,也要在字典实参前面加上两个*,告诉python参数dict是包裹关键字传递所用的字典。

包裹的注意的点,是在相应的元组和字典前面加上“”和“*


解包裹

先看例子:

1
2
3
4
5
6
7
8
9
10
11
>>> def func(a,b,c):
... print(a,b,c)
...
>>> args = ('1','2','3')
>>> func(*args)
1 2 3
>>> args = ('1','2','3','4','5')
>>> func(*args)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 3 positional arguments but 5 were given

我们首先定义了一个函数func(),向函数中传递三个参数,我们使用一个元组将要传递的实参包裹起来,整个传递到函数中,在元组前面加一个”*”,用于解包裹,将参数传递进这个函数,如果元组的元素的个数和函数的参数个数不匹配,就会报错。

类似的,使用字典包裹关键字传递也可以使用解包裹的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> def func(a,b,c):
... print(type(a))
... print("a="+str(a))
... print(type(b))
... print("b="+str(b))
... print(type(c))
... print("c="+str(c))
... print(a,b,c)
...
>>> mydict = dict(a=1,b=2,c=3)
>>> func(mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() missing 2 required positional arguments: 'b' and 'c'
>>> func(**mydict)
<class 'int'>
a=1
<class 'int'>
b=2
<class 'int'>
c=3
1 2 3
>>> mydict = dict(a=1,b=2,c=3,d=4)
>>> func(**mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() got an unexpected keyword argument 'd'


常用的向函数中传递参数的方法就这么几种,以后有新的体会再与大家分享。