传参和递归

2018-01-05  本文已影响0人  C1awn_

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)
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)
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
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}
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
#!/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)
上一篇 下一篇

猜你喜欢

热点阅读