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
xcc7624
V2EX  ›  Python

初学 python 求助个问题。

  •  
  •   xcc7624 · 2014-07-11 17:02:34 +08:00 · 3380 次点击
    这是一个创建于 3583 天前的主题,其中的信息可能已经有所发展或是发生改变。
    solution = []
    S = [[],[1]]

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

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

    print solution

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

    list_b = list_a.sort(),

    list_b 会是 (None, None) 哈哈。。。
    xcc7624
        8
    xcc7624  
    OP
       2014-07-11 22:27:32 +08:00
    @est一般我赖得看API都是用help函数,但是help函数显示出来的没强调函数的返回值。
    est
        9
    est  
       2014-07-11 23:29:09 +08:00   ❤️ 1
    @xcc7624 有返回文档里会说,文档里没说就没返回,没返回的都是None。 (这个比较坑。。。)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   921 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:10 · PVG 07:10 · LAX 16:10 · JFK 19:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.