PDF版 ePub版

# 函数(3)

## 参数收集

def func(x,*arg):
print x         #输出参数 x 的值
result = x
print arg       #输出通过 *arg 方式得到的值
for i in arg:
result +=i
return result

print func(1,2,3,4,5,6,7,8,9)    #赋给函数的参数个数不仅仅是  2个

1                       #这是函数体内的第一个 print，参数x得到的值是 1
(2, 3, 4, 5, 6, 7, 8, 9) #这是函数内的第二个 print，参数 arg 得到的是一个元组
45                      #最后的计算结果

• 值 1 传给了参数 x
• 值 2,3,4,5,6.7.8.9 被塞入一个 tuple 里面，传给了 arg

>>> def foo(*args):
...     print args      #打印通过这个参数得到的对象
... 

>>> foo(1,2,3)
(1, 2, 3)

>>> foo("qiwsir","qiwsir.github.io","python")
('qiwsir', 'qiwsir.github.io', 'python')

>>> foo("qiwsir",307,["qiwsir",2],{"name":"qiwsir","lang":"python"})
('qiwsir', 307, ['qiwsir', 2], {'lang': 'python', 'name': 'qiwsir'})

>>> foo("python")
('python',)

>>> def foo(x, *args):
...     print "x:",x
...     print "tuple:",args
...
>>> foo(7)
x: 7
tuple: ()

>>> def foo(**kargs):
...     print kargs
...
>>> foo(a=1,b=2,c=3)    #注意观察这次赋值的方式和打印的结果
{'a': 1, 'c': 3, 'b': 2}

>>> foo(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 0 arguments (3 given)

>>> def foo(x,y,z,*args,**kargs):
...     print x
...     print y
...     print z
...     print args
...     print kargs
...
>>> foo('qiwsir',2,"python")
qiwsir
2
python
()
{}
>>> foo(1,2,3,4,5)
1
2
3
(4, 5)
{}
>>> foo(1,2,3,4,5,name="qiwsir")
1
2
3
(4, 5)
{'name': 'qiwsir'}

## 另外一种传值方式

>>> def add(x,y):
...     return x + y
...
5

>>> bars = (2,3)
5

>>> bars = (2,3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add() takes exactly 2 arguments (3 given)

>>> def book(author,name):
...     print "%s is writing %s" % (author,name)
...
>>> bars = {"name":"Starter learning Python","author":"Kivi"}
>>> book(**bars)
Kivi is writing Starter learning Python

## 复习

python 中函数的参数通过赋值的方式来传递引用对象。下面总结通过总结常见的函数参数定义方式，来理解参数传递的流程。

### def foo(p1,p2,p3,...)

>>> def foo(p1,p2,p3):
...     print "p1==>",p1
...     print "p2==>",p2
...     print "p3==>",p3
...
>>> foo("python",1,["qiwsir","github","io"])    #一一对应地赋值
p1==> python
p2==> 1
p3==> ['qiwsir', 'github', 'io']

>>> foo("python")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 3 arguments (1 given)    #注意看报错信息

>>> foo("python",1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 3 arguments (4 given)    #要求 3 个参数，实际上放置了 4 个，报错

### def foo(p1=value1,p2=value2,...)

>>> foo(p3=3,p1=10,p2=222)
p1==> 10
p2==> 222
p3==> 3

>>> def foo(p1,p2=22,p3=33):    #设置了两个参数 p2,p3 的默认值
...     print "p1==>",p1
...     print "p2==>",p2
...     print "p3==>",p3
...
>>> foo(11)     #p1=11，其它的参数为默认赋值
p1==> 11
p2==> 22
p3==> 33
>>> foo(11,222)     #按照顺序，p2=222,p3 依旧维持原默认值
p1==> 11
p2==> 222
p3==> 33
>>> foo(11,222,333)  #按顺序赋值
p1==> 11
p2==> 222
p3==> 333

>>> foo(11,p2=122)
p1==> 11
p2==> 122
p3==> 33

>>> foo(p2=122)     #p1 没有默认值，必须要赋值的，否则报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes at least 1 argument (1 given)

### def foo(*args)

>>> def foo(*args):         #接收不确定个数的数据对象
...     print args
...
>>> foo("qiwsir.github.io") #以 tuple 形式接收到，哪怕是一个
('qiwsir.github.io',)
>>> foo("qiwsir.github.io","python")
('qiwsir.github.io', 'python')

#### def foo(**args)

>>> def foo(**args):    #这种方式接收，以 dictionary 的形式接收数据对象
...     print args
...

>>> foo(1,2,3)          #这样就报错了
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 0 arguments (3 given)

>>> foo(a=1,b=2,c=3)    #这样就可以了，因为有了键值对
{'a': 1, 'c': 3, 'b': 2}

>>> def foo(x,y=2,*targs,**dargs):
...     print "x==>",x
...     print "y==>",y
...     print "targs_tuple==>",targs
...     print "dargs_dict==>",dargs
...

>>> foo("1x")
x==> 1x
y==> 2
targs_tuple==> ()
dargs_dict==> {}

>>> foo("1x","2y")
x==> 1x
y==> 2y
targs_tuple==> ()
dargs_dict==> {}

>>> foo("1x","2y","3t1","3t2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {}

>>> foo("1x","2y","3t1","3t2",d1="4d1",d2="4d2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {'d2': '4d2', 'd1': '4d1'}