V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 4110 views
    This topic created in 4317 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
        1
    hhbcarl  
       Jul 11, 2014   ❤️ 1
    [].append修改了[]对象,返回None,所以结果就是这样
    yelite
        2
    yelite  
       Jul 11, 2014   ❤️ 1
    list.append 没有返回值,直接 [].append(9) 是没有作用的
    9hills
        3
    9hills  
       Jul 11, 2014   ❤️ 1
    [].append(9)的返回值是None
    xcc7624
        4
    xcc7624  
    OP
       Jul 11, 2014
    明白了,当成javascript的链式调用了。一般javascrip这种操作都会 在最后 returne this。
    est
        5
    est  
       Jul 11, 2014
    @xcc7624 估计你还会碰到 list_b = list_a.sort() 的坑。
    kaneg
        6
    kaneg  
       Jul 11, 2014   ❤️ 1
    @est 我碰到一个隐藏了5年的bug就是这个坑
    est
        7
    est  
       Jul 11, 2014   ❤️ 1
    @kaneg 再来一个坑:

    list_b = list_a.sort(),

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