用 closure 实现的叠加器(counter)出错,求解释原因 - 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
wenLiangcan

用 closure 实现的叠加器(counter)出错,求解释原因

  •  
  •   wenLiangcan Apr 8, 2014 3349 views
    This topic created in 4423 days ago, the information mentiond may be changed or developed.
    (似乎没法分开显示不同的文件。。。)

    看了这篇文章(http://coolshell.cn/articles/10739.html),有一个 Lua 写的 counter,觉得有趣:
    https://gist.github.com/wenLiangcan/10135400#file-counter-lua

    于是学着用 Python 写了一下,可是出错了:
    https://gist.github.com/wenLiangcan/10135400#file-er_counter-py

    搜索后看到这篇文章(http://jjinux.blogspot.com/2006/10/python-modifying-counter-in-closure.html),说把记录计数的变量换做列表就可以了:
    https://gist.github.com/wenLiangcan/10135400#file-counter-py

    请问这是为什么呢?上面文章提到的“to box variables within a closure”又是什么意思?



    谢谢
    7 replies    1970-01-01 08:00:00 +08:00
    monsterxx03
        1
    monsterxx03  
       Apr 8, 2014   2
    python闭包的经典问题

    i+=1 等价于i = i + 1, 语句从左向右执行,先是`i=`, 注意这里就覆盖了外部的i,实际上外部的i已经不可见了,然后`i+1`, 但此时外部的i在当前作用域中已经不可见,内部的i还未创建完成,等号右边的还没执行完成呢。。。所以会找不到这个i。

    lua能过,应该是语法解释器内部的逻辑和python不一样吧。
    wenLiangcan
        2
    wenLiangcan  
    OP
       Apr 8, 2014
    @monsterxx03 那为什么列表又不会被覆盖呢?
    monsterxx03
        3
    monsterxx03  
       Apr 9, 2014
    因为 i= xxx是在定义变量, i[0] =xxx 不是定义,只是赋值操作。
    wenLiangcan
        4
    wenLiangcan  
    OP
       Apr 9, 2014 via Android
    @monsterxx03 原来如此!
    josephok
        5
    josephok  
       Apr 12, 2014
    试试:
    <script src="https://gist.github.com/josephok/10524138.js"></script>
    josephok
        6
    josephok  
       Apr 12, 2014   1
    wenLiangcan
        7
    wenLiangcan  
    OP
       Apr 15, 2014
    @josephok 原来还有 nonlocal 这个关键字,学习了,v2ex 竟然没有提醒,现在才看到。。。
    About     Help     Advertise     Blog     API     FAQ     Solana     1215 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 23:19 PVG 07:19 LAX 16:19 JFK 19:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86