python 魔术方法
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