1. 传参数
1. 向函数传递元祖
现有一个函数(三个形参),元祖t1/t2
In [2]: def fun(x,y,z):
...: return x+y+z
...:
In [3]: t1 = (8,9)
In [4]: t2 = (1,2,3)
- 直接给函数传入元祖,都报错:只给了一个实参
In [5]: fun(t1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-2bbba3eac7d2> in <module>()
----> 1 fun(t1)
TypeError: fun() takes exactly 3 arguments (1 given)
In [6]: fun(t2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-be9a8abf3158> in <module>()
----> 1 fun(t2)
TypeError: fun() takes exactly 3 arguments (1 given)
- 元祖前加一个*,t2输出结果,t1因只有2个元素,报错
In [7]: fun(*t2)
Out[7]: 6
In [8]: fun(*t1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-fe16dde56c20> in <module>()
----> 1 fun(*t1)
TypeError: fun() takes exactly 3 arguments (2 given)
- 额外再给一个参数,t1满足3个参数了,但是t2多了1个参数
In [9]: fun(5,*t1)
Out[9]: 22
In [10]: fun(5,*t2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-3a341823cc01> in <module>()
----> 1 fun(5,*t2)
TypeError: fun() takes exactly 3 arguments (4 given)
- 给定参数放在元祖后面,报错
In [11]: fun(*t1,5)
File "<ipython-input-11-63abfbf858fe>", line 1
fun(*t1,5)
SyntaxError: only named arguments may follow *expression
2.传递字典
In [1]: def fun(x,y,z):
...: return x+y+z
...:
In [2]: dic = {'x':1,'y':3,'z':5}
In [3]: fun(**dic) #用**传递字典
Out[3]: 9
- 给字典换一个参数,y换成c,报错。因为c不是函数形参。但是顺序可以换。
In [4]: dic = {'x':1,'y':3,'c':5}
In [5]: fun(**dic)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-f4f83e494122> in <module>()
----> 1 fun(**dic)
TypeError: fun() got an unexpected keyword argument 'c'
2. 冗余参数
定义:def fun(x,y,*args,**kwargs)
args 和 kwargs 是约定俗成的冗余参数命名。
In [7]: def fun (x,*args,**kwargs):
...: print x
...: print args
...: print kwargs
...:
In [8]: fun()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-69e6a439c52d> in <module>()
----> 1 fun()
TypeError: fun() takes at least 1 argument (0 given)
#因为事先已经定义了一个形参
In [9]: fun(1,2,3)
1
(2, 3)
{}
In [10]: fun(1,2,3,[1,2])
1
(2, 3, [1, 2])
{}
这说明:第一个实参对应函数定义的形参,之后的几个都对应args
In [11]: fun(1,2,3,[1,2],a=100)
1
(2, 3, [1, 2])
{'a': 100}
kwargs需要a=100这样赋值,给kwargs多传几个参数:
In [2]: def fun (x,*args,**kwargs):
print x
print args
print kwargs
...:
In [3]: fun(1,2,3,[1,2],a=100,'y'=5)
File "<ipython-input-3-b0f139033a2d>", line 1
fun(1,2,3,[1,2],a=100,'y'=5)
SyntaxError: keyword can't be an expression
In [4]: fun(1,2,3,[1,2],a=100,y=5)
1
(2, 3, [1, 2])
{'a': 100, 'y': 5}
- 更进一步:既然args输出的是元祖,那传参可以用*元祖。kwargs输出的是字典,传参可以用**字典:
In [4]: fun(1,2,3,[1,2],a=100,y=5)
1
(2, 3, [1, 2])
{'a': 100, 'y': 5}
In [5]: fun(1,2,3,[1,2],a=100,y=5,*(8,8),**{'o':4,'p':9})
1
(2, 3, [1, 2], 8, 8)
{'y': 5, 'p': 9, 'a': 100, 'o': 4}
3. 递归调用
在没有用递归调用前,举一个代码例子,计算1到100的和:
def factorial(n):
sum = 0
for i in range(1,n+1): #1到n
sum += i
return sum
print factorial(100)
结果:
C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180105/cs.py
5050
Process finished with exit code 0
- 递归调用比喻:假定f(3)=3*2*1,f(2)=2*1,f(1)=1*1.f(0)=1.那f(3)=3*f(2),f(2)=2*f(1),f(1)=1*f(0)
- 递归必须有最后的默认结果,比如上面的f(0)
- 递归参数必须向默认结果收敛的: factorial(n-1)
求和函数可改写为:
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
def factorial(n):
if n == 0:
return 0 #f(0)=0
else:
return n + factorial(n-1)
print factorial(100)
网友评论