码迷,mamicode.com
首页 > 编程语言 > 详细

python学习之---函数进阶

时间:2015-02-16 18:18:37      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

一,递归函数:

  做程序应该都知道,在一个函数的内部还可以调用其它函数,这叫函数的调用,但是有一种特殊的情况,在一个函数内部对自身函数的调用,我们成这为函数的递归调用。

在此,使用一个家喻户晓的例子来演示一下函数的递归调用------求阶乘:

1 >>> func(1)
2 1
3 >>> func(10)
4 3628800
5 >>> func(100)
6 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
7 >>> func(200)
8 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000L
9 >>> 

该函数实现了对自己的反复调用,这就时递归!

递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。

使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出。可以试试func(1000):这就是栈溢出的异常显示

 1   File "<console>", line 4, in func
 2   File "<console>", line 4, in func
 3   File "<console>", line 4, in func
 4   File "<console>", line 4, in func
 5   File "<console>", line 4, in func
 6   File "<console>", line 4, in func
 7   File "<console>", line 4, in func
 8   File "<console>", line 4, in func
 9   File "<console>", line 4, in func
10   File "<console>", line 4, in func
11   File "<console>", line 4, in func
12   File "<console>", line 4, in func
13   File "<console>", line 4, in func
14   File "<console>", line 4, in func
15   File "<console>", line 4, in func
16   File "<console>", line 4, in func
17   File "<console>", line 4, in func
18   File "<console>", line 4, in func
19   File "<console>", line 4, in func
20   File "<console>", line 4, in func
21   File "<console>", line 4, in func
22   File "<console>", line 4, in func
23   File "<console>", line 4, in func
24   File "<console>", line 4, in func

解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。

尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。

上面的fact(n)函数由于return n * fact(n - 1)引入了乘法表达式,所以就不是尾递归了。要改成尾递归方式,需要多一点代码,主要是要把每一步的乘积传入到递归函数中:

1 def func(n):
2     return func_iter(1, 1, n)
3 
4 def func_iter(product, count, max):
5     if count > max:
6         return product
7     return func_iter(product * count, count + 1, max)

尾递归调用时,如果做了优化,栈不会增长,因此,无论多少次调用也不会导致栈溢出。

遗憾的是,大多数编程语言没有针对尾递归做优化,Python解释器也没有做优化,所以,即使把上面的fact(n)函数改成尾递归方式,也会导致栈溢出。

有一个针对尾递归优化的decorator,可以参考源码:

http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/

我们后面会讲到如何编写decorator。现在,只需要使用这个@tail_call_optimized,就可以顺利计算出fact(1000)

代码描述如下:

 1 #!/usr/bin/env python2.4
 2 # This program shows off a python decorator(
 3 # which implements tail call optimization. It
 4 # does this by throwing an exception if it is 
 5 # it‘s own grandparent, and catching such 
 6 # exceptions to recall the stack.
 7 
 8 import sys
 9 
10 class TailRecurseException:
11   def __init__(self, args, kwargs):
12     self.args = args
13     self.kwargs = kwargs
14 
15 def tail_call_optimized(g):
16   """
17   This function decorates a function with tail call
18   optimization. It does this by throwing an exception
19   if it is it‘s own grandparent, and catching such
20   exceptions to fake the tail call optimization.
21   
22   This function fails if the decorated
23   function recurses in a non-tail context.
24   """
25   def func(*args, **kwargs):
26     f = sys._getframe()
27     if f.f_back and f.f_back.f_back 28         and f.f_back.f_back.f_code == f.f_code:
29       raise TailRecurseException(args, kwargs)
30     else:
31       while 1:
32         try:
33           return g(*args, **kwargs)
34         except TailRecurseException, e:
35           args = e.args
36           kwargs = e.kwargs
37   func.__doc__ = g.__doc__
38   return func
39 
40 @tail_call_optimized
41 def factorial(n, acc=1):
42   "calculate a factorial"
43   if n == 0:
44     return acc
45   return factorial(n-1, n*acc)
46 
47 print factorial(10000)
48 # prints a big, big number,
49 # but doesn‘t hit the recursion limit.
50 
51 @tail_call_optimized
52 def fib(i, current = 0, next = 1):
53   if i == 0:
54     return current
55   else:
56     return fib(i - 1, next, current + next)
57 
58 print fib(10000)
59 # also prints a big number,
60 # but doesn‘t hit the recursion limit.

 

python学习之---函数进阶

标签:

原文地址:http://www.cnblogs.com/blogofwyl/p/4294407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!