python学习之---函数进阶
一,递归函数:
做程序应该都知道,在一个函数的内部还可以调用其它函数,这叫函数的调用,但是有一种特殊的情况,在一个函数内部对自身函数的调用,我们成这为函数的递归调用。
在此,使用一个家喻户晓的例子来演示一下函数的递归调用------求阶乘:
>>> func(1)
1
>>> func(10)
3628800
>>> func(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L
>>> func(200)
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000L
>>>
该函数实现了对自己的反复调用,这就时递归!
递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。
使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出。可以试试func(1000)
:这就是栈溢出的异常显示
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
File "<console>", line 4, in func
解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。
尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。
上面的fact(n)
函数由于return n * fact(n - 1)
引入了乘法表达式,所以就不是尾递归了。要改成尾递归方式,需要多一点代码,主要是要把每一步的乘积传入到递归函数中:
def func(n):
return func_iter(1, 1, n) def func_iter(product, count, max):
if count > max:
return product
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)
:
代码描述如下:
#!/usr/bin/env python2.4
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is
# it's own grandparent, and catching such
# exceptions to recall the stack. import sys class TailRecurseException:
def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs def tail_call_optimized(g):
"""
This function decorates a function with tail call
optimization. It does this by throwing an exception
if it is it's own grandparent, and catching such
exceptions to fake the tail call optimization. This function fails if the decorated
function recurses in a non-tail context.
"""
def func(*args, **kwargs):
f = sys._getframe()
if f.f_back and f.f_back.f_back \
and f.f_back.f_back.f_code == f.f_code:
raise TailRecurseException(args, kwargs)
else:
while 1:
try:
return g(*args, **kwargs)
except TailRecurseException, e:
args = e.args
kwargs = e.kwargs
func.__doc__ = g.__doc__
return func @tail_call_optimized
def factorial(n, acc=1):
"calculate a factorial"
if n == 0:
return acc
return factorial(n-1, n*acc) print factorial(10000)
# prints a big, big number,
# but doesn't hit the recursion limit. @tail_call_optimized
def fib(i, current = 0, next = 1):
if i == 0:
return current
else:
return fib(i - 1, next, current + next) print fib(10000)
# also prints a big number,
# but doesn't hit the recursion limit.
python学习之---函数进阶的更多相关文章
- python学习总结(函数进阶)
-------------------程序运行原理------------------- 1.模块的内建__name__属性,主模块其值为__main__,导入模块其值为模块名 1.创建时间, ...
- Python学习之函数进阶
函数的命名空间 著名的python之禅 Beautiful is better than ugly. Explicit is better than implicit. Simple is bette ...
- Python学习day15-函数进阶(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day14-函数进阶(2)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day13-函数进阶(1)
Python学习day13-函数进阶(1) 闭包函数 闭包函数,从名字理解,闭即是关闭,也就是说把一个函数整个包起来.正规点说就是指函数内部的函数对外部作用域而非全局作用域的引用. 为函数传参的方式有 ...
- python学习8—函数之高阶函数与内置函数
python学习8—函数之高阶函数与内置函数 1. 高阶函数 a. map()函数 对第二个输入的参数进行第一个输入的参数指定的操作.map()函数的返回值是一个迭代器,只可以迭代一次,迭代过后会被释 ...
- python学习7—函数定义、参数、递归、作用域、匿名函数以及函数式编程
python学习7—函数定义.参数.递归.作用域.匿名函数以及函数式编程 1. 函数定义 def test(x) # discription y = 2 * x return y 返回一个值,则返回原 ...
- 从0开始的Python学习007函数&函数柯里化
简介 函数是可以重用的程序段.首先这段代码有一个名字,然后你可以在你的程序的任何地方使用这个名称来调用这个程序段.这个就是函数调用,在之前的学习中我们已经使用了很多的内置函数像type().range ...
- 【python 3】 函数 进阶
函数进阶 1.函数命名空间和作用域 命名空间一共分为三种: 全局命名空间 局部命名空间 内置命名空间 *内置命名空间中存放了python解释器为我们提供的名字:input , print , str ...
随机推荐
- C#泛型比较大小
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 实现当UILable的内容超出其范围后自动滚动效果
本文主要介绍 [当UILabel的内容超出其自身的宽度范围后,进行互动展示的效果],我们先来看一下Demo的效果图. 实际实现起来并不十分繁杂,在这里,为了开发的效率,我们使用了一个已经封装好的UIL ...
- HTTP请求、响应报文格式
HTTP请求报文格式: HTTP请求报文主要由请求行.请求头部.空行以及请求正文4部分组成 1,请求行由3部分组成,分别为:请求方式,URI(注意这里不是URL)以及协议版本组成,之间由空格分隔 请求 ...
- ZooKeeper 分布式锁实现
1 场景描述 在分布式应用, 往往存在多个进程提供同一服务. 这些进程有可能在相同的机器上, 也有可能分布在不同的机器上. 如果这些进程共享了一些资源, 可能就需要分布式锁来锁定对这些资源的访问. 2 ...
- 查询制定行数的数据(2)对了,mysql不能用top关键字
采用嵌套查询的方式,倒序之后前10条 倒序之后前9条 采用嵌套查询的方式,倒序之后前10条 排正序之后从第一条开始弄十条数据 排正序之后从第一条开始弄九条数据 排正序之后从第十条开始弄十条数据 排正序 ...
- 关于ContentProvider的批量操作
今天看公司代码,发现在批量插入通话记录和联系人的时候,用了一个 ArrayList<ContentProviderOperation> ops = new ArrayList<Con ...
- Ubuntu 使用top/free查看内存占用大的原因
Ubuntu 使用top/free查看内存占用大的原因 linux/ubuntu下free/top查看内存占用大的原因 使用free/top查看内存占用的时候,吓了一大跳,机器4GB的内存,显 ...
- 我的微软.net演进路线图
原文地址:我的微软.net演进路线图 我的微软.net演进路线图 我的这几年,编程方面主要是跟在微软旗下奔跑的,主要语言是C# 集成开发环境(IDE) .NET Framework版本 介入年份 Vi ...
- ios隐藏导航栏底线条和导航、状态栏浙变色
方法一遍历法: 在你需要隐藏的地方调用如下代码 [self findlineviw:self.navigationBar].hidden = YES; -(UIImageView*)findlinev ...
- 使用Eclipse提供的Axis1.x生成WSDL文件以及Server和Client代码
使用Eclipse自带的Axis 1.x来创建一个web service应用的服务端和客户端 Axis 是SOAP WebService协议实现,SOAP实质上是一个基于HTTP POST的请求,以X ...