Skip to content

Instantly share code, notes, and snippets.

@iamsunxin
Created July 20, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamsunxin/b1de234354fafeb91f5fb890e433bd0a to your computer and use it in GitHub Desktop.
Save iamsunxin/b1de234354fafeb91f5fb890e433bd0a to your computer and use it in GitHub Desktop.
关于程序执行顺序的一点疑惑
import logging
from time import time as t
import time
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
is_debug = True
def count_time(is_debug):
def handle_func(func):
def handle_args(*args, **kwargs):
if is_debug:
begin = t()
func(*args, **kwargs)
print 'args2','kwargs2',args,kwargs
logging.debug( "[" + func.__name__ + "] -> " + str(t() - begin) )
else:
func(*args, **kwargs)
print 'handle_args', handle_args
print type(handle_args)
return handle_args
return handle_func
def pr():
for i in range(1,1000):
i = i * 2
time.sleep(0.01)
print "hello world",t()
def test():
pr()
print 'test'
@count_time(is_debug)
def test2():
pr()
print 'test2'
@count_time(False)
def test3():
pr()
print 'test3'
if __name__ == "__main__":
test()
test2()
test3()
结果
C:\Python27\python.exe D:/python/pyworkspcae/testdeco/timedeco.py
hello world 1500518211.89
test
hello world 1500518221.92
DEBUG:root:[test2] -> 10.0350000858
test2
args2 kwargs2 () {}
hello world 1500518232.03
test3
handle_args <function handle_args at 0x00000000029C6518>
<type 'function'>
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment