python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
来源:互联网 发布:朋友圈广告 知乎 编辑:程序博客网 时间:2024/05/13 04:00
python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
def memo(func): cache = {} def wrapper(*args): if args not in cache: cache[args] = func(*args) return cache[args] return wrapper
def memo(func): cache = {} def wrapper(*args): if args not in cache: cache[args] = func(*args) return cache[args] return wrapper@memodef fibonacci(n): if n <= 1: return 1 return fibonacci(n - 1) + fibonacci(n - 2)def fibonacci2(n,cache = None): if cache is None: cache = {} if n in cache: return cache[n] if n <= 1: return 1 cache[n] = fibonacci2(n - 1,cache) + fibonacci2(n - 2,cache) return cache[n]==>一个共有10个台阶的楼梯,从下面走到上面,一次只能迈1,2,3个台阶==> 并且不能后退,走完这个楼梯共有多少中方法def climb(n,steps): count = 0 if n == 0: count = 1 elif n >0: for step in steps: count += climb(n-step,steps) return count print fibonacci2(5)print fibonacci(50)fibonacci = memo(fibonacci)print fibonacci(50)print climb(10,(1,2,3))
0 0
- python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
- 请教:python装饰器如何不改变原函数名
- PYTHON 函数装饰器
- python 函数装饰器
- Python函数:装饰器
- Python 函数装饰器
- python函数装饰器
- python函数装饰器
- python 装饰器函数
- python装饰器的4中类型:函数装饰函数、函数装饰类、类装饰函数、类装饰类
- python 使用函数装饰器的例子
- python-函数装饰器的使用-25
- python函数装饰器的使用
- Python函数、装饰器(基础整理)
- python 9-3 如何定义带参数的装饰器,提取函数签名python3 inspect.signature()
- python 9-2 如何为被装饰的函数保存元数据,使用标准库functools中的装饰器wraps 装饰内部包裹函数
- Python 函数装饰器探讨
- python函数之一 装饰器
- Cocos2d-x 3.x 图形学渲染系列二十七
- 《HelloGitHub月刊》第10期
- 如何求m的n次方(结果很大)
- 《领域驱动设计》阅读笔记 第1章 消化知识
- 171. Excel Sheet Column Number
- python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
- 【c基础】之 文件及其操作
- php decode(100)
- DIV盒模型和伪类-tap选项
- string中c_str()、data()、copy(p,n)函数的用法+strstr()函数用法
- 【G】开源的分布式部署解决方案(二)
- Windows7下安装Caffe(GPU):试了很久的失败版本(可参考)
- bzoj2631
- 【LeetCode题解】二叉树的遍历