您的位置:首页 > 编程语言 > Python开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: