碰见个程序执行顺序的问题,大婶们帮忙看下

2017-07-20 10:44:40 +08:00
 iamsunxin
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


DEBUG 输出这条信息顺序看不懂
2787 次点击
所在节点    Python
14 条回复
crayygy
2017-07-20 11:08:51 +08:00
作为一个靠缩进的语言,你这样子发别人很难看的清楚,建议发个 gist,或者至少也要注意一下排版
NoAnyLove
2017-07-20 13:25:45 +08:00
我纯粹是被标题吸引进来的,就想看看什么大婶这么厉害,还会编程,中关村扫地大婶?

另外,没缩进的 Python 代码我根本读不懂。。。。。。
Sapp
2017-07-20 13:29:58 +08:00
那些说 python 缩进设计好的人呢?来来来,你来读。
soratadori
2017-07-20 13:39:00 +08:00
@Sapp 如果这个是个不靠缩进的语言你读不读
AEANWspPmj3FUhDc
2017-07-20 13:41:07 +08:00
我是小可爱,不是大婶
iamsunxin
2017-07-20 17:16:18 +08:00
@crayygy 抱歉,没注意排版就发了,补一个 gist https://gist.github.com/iamsunxin/b1de234354fafeb91f5fb890e433bd0a
iamsunxin
2017-07-20 17:17:28 +08:00
@NoAnyLove 扫地的一般都是拆迁的壕,补个 gist 见上条回复
crayygy
2017-07-20 17:18:19 +08:00
@Sapp 即使是 C,这种缩进编译器能过,人看的时候也遭不住...
iamsunxin
2017-07-20 17:20:16 +08:00
@ivlioioilvi 给你抓把糖吧
ClutchBear
2017-07-20 18:07:54 +08:00
有啥问题,
这是典型装饰器啊.

简单理解,就是
@count_time(is_debug)
def test2():
pr()
print 'test2'
ClutchBear
2017-07-20 18:09:53 +08:00
这是典型装饰器啊.

简单理解,就是
@count_time(is_debug)
def test2():
pr()
print 'test2'

单实际执行的是,
def count_time(is_debug):
def handle_func(test2):
def handle_args(*args, **kwargs):
if is_debug:
begin = t()
test2(*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
mdzz
2017-07-20 18:24:24 +08:00
logging.debug 按代码是输出到了 stderr,print 输出到 stdout
而一般 stdout 是带缓冲的,stderr 不带缓冲立即输出

LZ 可以试试把 stdout 和 stderr 分开输出
python.exe timedeco.py >stdout.txt 2>stderr.txt
iamsunxin
2017-07-20 22:10:47 +08:00
@mdzz 一言明了,感谢感谢
NoAnyLove
2017-07-21 05:58:49 +08:00
@Sapp 缩进本来就是 Python 语言的一部分啊。来来来,看你是用 Java/C/C++/C#还是用什么的,你把花括号全去掉了(也是去掉语言的一部分啊),读出来看看呢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/376611

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX