列表操作遇到了一个奇怪的问题

120 views
Skip to first unread message

LetterK

unread,
Jan 20, 2018, 11:32:21 AM1/20/18
to python-cn(华蟒用户组,CPyUG 邮件列表)
list1 = []
list2 = list1.append(0)
print(list2)
print(list1.append(0))
print(list1)

结果:
None
None
[0, 0]

让我很困惑
为何列表操作的结果不能放进变量里?

Ted Gao

unread,
Jan 20, 2018, 11:40:52 AM1/20/18
to pyth...@googlegroups.com
The append method of list updates the list in place, and does not return ant value.



On Jan 20, 2018, at 23:32, LetterK <kis...@gmail.com> wrote:

list1.append(0)

andrew

unread,
Jan 20, 2018, 12:09:36 PM1/20/18
to pyth...@googlegroups.com
看上去挺正常啊
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要发帖到此群组,请发送电子邮件至pyth...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

laike9m

unread,
Jan 20, 2018, 12:12:29 PM1/20/18
to pyth...@googlegroups.com
append 不返回东西,Go 写多了吧

Hua Liang

unread,
Jan 21, 2018, 1:47:47 AM1/21/18
to python-cn
谢谢回答和指路

在 2018年1月21日 上午1:11,laike9m <lai...@gmail.com>写道:
append 不返回东西,Go 写多了吧

On Sun, Jan 21, 2018 at 1:09 AM andrew <wangchua...@gmail.com> wrote:
看上去挺正常啊

On 六, 2018-01-20 at 23:40 +0700, Ted Gao wrote:
The append method of list updates the list in place, and does not return ant value.



On Jan 20, 2018, at 23:32, LetterK <kis...@gmail.com> wrote:

list1.append(0)

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上“python-cn(华蟒用户组,CPyUG 邮件列表)”群组中的主题。
要退订此主题,请访问https://groups.google.com/d/topic/python-cn/-u9PyLPmCmI/unsubscribe
要退订此群组及其所有主题,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

laixintao

unread,
Jan 24, 2018, 9:10:14 AM1/24/18
to python-cn(华蟒用户组,CPyUG 邮件列表)
这个问题我也有过困惑,dict.update 也是不返回的,那么啥时候返回啥时候不返回啊?

后来明白了,这是Python的设计哲学

You might have noticed that methods like insertremove or sort that only modify the list have no return value printed – they return the default None[1] This is a design principle for all mutable data structures in Python.

这样你是用这些方法的时候,就会很明确这些方法到底是在修改对象本身,还是在构建新的对象了! 像别的语言,你使用内置对象的时候都要对内部的原理很明白才能写出好代码,就比较反人类了。

import this

There should be one-- and preferably only one --obvious way to do it.



在 2018年1月21日星期日 UTC+8上午12:32:21,LetterK写道:
Reply all
Reply to author
Forward
0 new messages