python 魔术方法
2015-07-19 23:05
645 查看
import time func_count_time_dict = {} def count_time(func): global func_count_time_dict func_count_time_dict[func]=[1,time.localtime()] def wrapper(): global func_count_time_dict func() print 'func name: ',func.__name__ print 'count: ',func_count_time_dict[func][0] print 'last call time: ',time.asctime(func_count_time_dict[func][1]) func_count_time_dict[func][0]+=1 func_count_time_dict[func][1]=time.localtime() return wrapper @count_time def foo(): print 'in foo()' @count_time def foo_1(): print 'in foo_1' foo_1() foo_1() foo_1() foo() foo_1() foo() foo() foo_1()
class MyIterator(object): def __init__(self, data_iter): self.data_iter = data_iter self.start = 0 def __iter__(self): return self def next(self): print self.start if self.start >= len(self.data_iter): raise StopIteration ans = self.data_iter[self.start] self.start += 1 return ans if __name__ == "__main__": iter = MyIterator(range(10)) for i in iter: print i
def fib(max): a, b = 0, 1 while a < max: yield a a, b = b, a + b for i in fib(1000): print(i)
map_output = map(lambda x: 2 * x, range(10)) print map_output print map(lambda x, y : x + y, range(8), range(8)) reduce_output = reduce(lambda x, y:x * y, range(1, 10)) ##pep8? print reduce_output m=lambda x,y:x>y print m(1,5) filter_output = filter(lambda x:x % 2, range(1, 10)) print filter_output
相关文章推荐
- Python lambda函数的用法
- python if语句
- Python笔记——break的注意事项
- Python笔记——break的注意事项
- 学习两天python后感
- 如何写出优雅的Python
- python 输出中文乱码问题
- python读取配置文件
- python的高级特性3:神奇的__call__与返回函数
- Berkeley's SICP in python3(三)
- pyenv管理python多版本
- Python解决Google文档打开缓慢问题
- python字典--知识点总结
- python字典--知识点总结
- Python dict 字典
- python程序中文输出问题怎么解决
- Python生成验证码
- python re模块(一)
- python下的pillow安装
- PyCharm设置python新建文件指定编码为utf-8