有点迷糊,headers_set[:] = [status, response_headers] - V2EX
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
hustlzp
V2EX    Python

有点迷糊,headers_set[:] = [status, response_headers]

  •  
  •   hustlzp 2013-08-27 14:58:46 +08:00 3328 次点击
    这是一个创建于 4504 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天在看关于WSGI的PEP333文档:

    http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side

    看到一行代码有点迷糊:

    headers_set[:] = [status, response_headers]

    我只知道list[:]的作用在于拷贝一个新的列表对象,一般用于防止2个变量引用到了一个对象,这样的话改变一方会影响到另一方的值,比如:

    L = [1, 2]
    def changer(L):
    L[0] = 2

    changer(L) # L = [2, 2]
    changer(L[:]) # L = [1, 2]

    但是 headers_set[:] = [status, response_headers] 却有点看不懂了。右边是一个新的对象,左边也是一个拷贝的新的对象,把一个对象给另一个对象赋值有意义吗?

    求大家指点!
    6 条回复    1970-01-01 08:00:00 +08:00
    SErHo
        1
    SErHo  
       2013-08-27 15:22:11 +08:00
    headers_set[:] 是这个函数外定义的,这个就是给 headers_set 赋值啊,举个例子如下:

    def test():
    ....h = []
    ....p = []

    ....def test_t():
    ........h = [1, 2]
    ........p[:] = [1, 2]
    ....test_t()
    ....print h
    ....print p
    hustlzp
        2
    hustlzp  
    OP
       2013-08-27 15:42:12 +08:00
    @SErHo 原来是这样...好怪...

    在stackoverflow上找到了一个相关的问题,但不是针对list,而是针对不可变的变量:

    http://stackoverflow.com/questions/8447947/is-it-possible-to-modify-variable-in-python-that-is-in-outer-but-not-global-sc

    python3的nonlocal关键字也可以解决这个“给外层函数中不可变的变量赋值”的问题:

    The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.
    SErHo
        3
    SErHo  
       2013-08-27 15:48:48 +08:00   1
    @hustlzp 其实我对这种作用域地方的问题也不太清楚,不过要搞明白为啥要这样用,写个例子来试试就知道了。
    hustlzp
        4
    hustlzp  
    OP
       2013-08-27 15:55:46 +08:00
    @SErHo 恩,谢谢!
    felix021
        5
    felix021  
       2013-08-27 16:13:44 +08:00   1
    @hustlzp 去了解一下python是怎么实现slice的,就会豁然开朗了。

    关键字: python __getitme__ __setitem__ slice

    TRY:

    class A(object):

    ..def __getitem__(self, key):
    ....print key
    ....return 0

    ..def __setitem__(self, key, value):
    ....print key, value

    a = A()
    print a[0:1]
    a[0:1] = [1]
    wynemo
        6
    wynemo  
       2013-08-27 21:40:12 +08:00   1
    直接在嵌套函数里些headers_set = [status, response_headers] 会认为在嵌套函数里新声明了一个headers_set 作用范围就在嵌套函数里 不改变外层headers_set的值
    headers_set[:] 这样是给headers_set赋值 会改变外层的值

    具体用locals看

    def test():
    ....a = []
    ....def test_t():
    ........print locals()
    ........a.extend([1,3])
    ....test_t()
    ....print a

    test()

    def test():
    ....a = []
    ....def test_t():
    ........print locals()
    ........a = [1, 3]
    ....test_t()
    ....print a

    test()
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5122 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 09:16 PVG 17:16 LAX 01:16 JFK 04:16
    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