//edit http://www.lai18.com //date 2015-06-21 def foo(): print ‘in foo()‘ foo()
//edit http://www.lai18.com //date 2015-06-21 import time def foo(): start = time.clock() print ‘in foo()‘ end = time.clock() print ‘used:‘, end - start foo()
//edit http://www.lai18.com //date 2015-06-21 import time def foo(): print ‘in foo()‘ def timeit(func): start = time.clock() func() end =time.clock() print ‘used:‘, end - start timeit(foo)
#-*- coding: UTF-8 -*- //edit http://www.lai18.com //date 2015-06-21 import time def foo(): print ‘in foo()‘ # 定义一个计时器,传入一个,并返回另一个附加了计时功能的方法 def timeit(func): # 定义一个内嵌的包装函数,给传入的函数加上计时功能的包装 def wrapper(): start = time.clock() func() end =time.clock() print ‘used:‘, end - start # 将包装后的函数返回 return wrapper foo = timeit(foo) foo()
import time def timeit(func): def wrapper(): start = time.clock() func() end =time.clock() print ‘used:‘, end - start return wrapper @timeitdef foo(): print ‘in foo()‘ foo()
class Rabbit(object): def __init__(self, name): self._name = name @staticmethod def newRabbit(name): return Rabbit(name) @classmethod def newRabbit2(cls): return Rabbit(‘‘) @property def name(self): return self._name
@name.setter def name(self, name): self._name = name
import time import functools def timeit(func): @functools.wraps(func) def wrapper(): start = time.clock() func() end =time.clock() print ‘used:‘, end - start return wrapper @timeitdef foo(): print ‘in foo()‘ foo()print foo.__name__
def total_ordering(cls): """Class decorator that fills in missing ordering methods""" convert = { ‘__lt__‘: [(‘__gt__‘, lambda self, other: other < self), (‘__le__‘, lambda self, other: not other < self), (‘__ge__‘, lambda self, other: not self < other)], ‘__le__‘: [(‘__ge__‘, lambda self, other: other <= self), (‘__lt__‘, lambda self, other: not other <= self), (‘__gt__‘, lambda self, other: not self <= other)], ‘__gt__‘: [(‘__lt__‘, lambda self, other: other > self), (‘__ge__‘, lambda self, other: not other > self), (‘__le__‘, lambda self, other: not self > other)], ‘__ge__‘: [(‘__le__‘, lambda self, other: other >= self), (‘__gt__‘, lambda self, other: not other >= self), (‘__lt__‘, lambda self, other: not self >= other)] } roots = set(dir(cls)) & set(convert) if not roots: raise ValueError(‘must define at least one ordering operation: < > <= >=‘) root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ for opname, opfunc in convert[root]: if opname not in roots: opfunc.__name__ = opname opfunc.__doc__ = getattr(int, opname).__doc__ setattr(cls, opname, opfunc) return cls
原文:http://blog.csdn.net/hello_katty/article/details/46591463