V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
iamsunxin
V2EX  ›  Python

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

  •  
  •   iamsunxin · 2017-07-20 10:44:40 +08:00 · 2772 次点击
    这是一个创建于 2469 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 输出这条信息顺序看不懂
    14 条回复    2017-07-21 05:58:49 +08:00
    crayygy
        1
    crayygy  
       2017-07-20 11:08:51 +08:00 via iPhone   ❤️ 1
    作为一个靠缩进的语言,你这样子发别人很难看的清楚,建议发个 gist,或者至少也要注意一下排版
    NoAnyLove
        2
    NoAnyLove  
       2017-07-20 13:25:45 +08:00
    我纯粹是被标题吸引进来的,就想看看什么大婶这么厉害,还会编程,中关村扫地大婶?

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

    简单理解,就是
    @count_time(is_debug)
    def test2():
    pr()
    print 'test2'
    ClutchBear
        11
    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
        12
    mdzz  
       2017-07-20 18:24:24 +08:00   ❤️ 2
    logging.debug 按代码是输出到了 stderr,print 输出到 stdout
    而一般 stdout 是带缓冲的,stderr 不带缓冲立即输出

    LZ 可以试试把 stdout 和 stderr 分开输出
    python.exe timedeco.py >stdout.txt 2>stderr.txt
    iamsunxin
        13
    iamsunxin  
    OP
       2017-07-20 22:10:47 +08:00
    @mdzz 一言明了,感谢感谢
    NoAnyLove
        14
    NoAnyLove  
       2017-07-21 05:58:49 +08:00
    @Sapp 缩进本来就是 Python 语言的一部分啊。来来来,看你是用 Java/C/C++/C#还是用什么的,你把花括号全去掉了(也是去掉语言的一部分啊),读出来看看呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1028 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:19 · PVG 06:19 · LAX 15:19 · JFK 18:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.