我有
try:
a = list().append('hello')
Run Code Online (Sandbox Code Playgroud)
但a为NoneType
try:
b = list()
b.append('hello')
Run Code Online (Sandbox Code Playgroud)
并且b是一种list类型
我认为list()返回一个列表对象,list().append('hello')并将使用返回列表进行追加,但为什么值为a None?
list()确实返回一个空列表([]),但该append方法在就地列表上运行- 它更改列表本身,并且不返回新列表.它返回了None.
例如:
>>> lst = []
>>> lst.append('hello') # appends 'hello' to the list
>>> lst
['hello']
>>> result = lst.append('world') # append method returns None
>>> result # nothing is displayed
>>> print result
None
>>> lst # the list contains 'world' as well now
['hello', 'world']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |