推荐学习书目
› 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
xcc7624
V2EX  ›  Python

初学 python 求助个问题。

  •  
  •   xcc7624 · Jul 11, 2014 · 4485 views
    This topic created in 4498 days ago, the information mentioned may be changed or developed.
    solution = []
    S = [[],[1]]

    for item in S:
    solution.append( [] )

    for item in S:
    solution.append( [].append(9) )

    print solution

    得到的结果是这:
    [[], [], None, None]
    为什么是这样
    9 replies  •  2014-07-11 23:29:09 +08:00
    hhbcarl
    hhbcarl
    Jul 11, 2014❤️1
    1
    [].append修改了[]对象,返回None,所以结果就是这样
    yelite
    yelite
    Jul 11, 2014❤️1
    2
    list.append 没有返回值,直接 [].append(9) 是没有作用的
    9hills
    9hills
    Jul 11, 2014❤️1
    3
    [].append(9)的返回值是None
    xcc7624
    xcc7624
    OP
    Jul 11, 2014
    4
    明白了,当成javascript的链式调用了。一般javascrip这种操作都会 在最后 returne this。
    est
    est
    Jul 11, 2014
    5
    @xcc7624 估计你还会碰到 list_b = list_a.sort() 的坑。
    kaneg
    kaneg
    Jul 11, 2014❤️1
    6
    @est 我碰到一个隐藏了5年的bug就是这个坑
    est
    est
    Jul 11, 2014❤️1
    7
    @kaneg 再来一个坑:

    list_b = list_a.sort(),

    list_b 会是 (None, None) 哈哈。。。
    xcc7624
    xcc7624
    OP
    Jul 11, 2014
    8
    @est一般我赖得看API都是用help函数,但是help函数显示出来的没强调函数的返回值。
    est
    est
    Jul 11, 2014❤️1
    9
    @xcc7624 有返回文档里会说,文档里没说就没返回,没返回的都是None。 (这个比较坑。。。)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4917 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 07:24 · PVG 15:24 · LAX 23:24 · JFK 02:24
    ♥ Do have faith in what you're doing.