ll = [].append(1) 会引发的问题

2018-04-01 11:14:09 +08:00
 limerence12138

只要打出这行,就会一直这样,然后内存也会一直增加。

4373 次点击
所在节点    Python
19 条回复
limerence12138
2018-04-01 11:15:56 +08:00
哪位兄弟来简单解释一下原因啊,是从对象,引用,这方面分析吗,或者是文档
roychan
2018-04-01 11:18:39 +08:00
append 本身就不返回任何值
picture2200
2018-04-01 11:19:32 +08:00
会不会是 pycharm 的问题
wellsc
2018-04-01 11:21:31 +08:00
LL = []
LL.append(1)
print(LL)
limerence12138
2018-04-01 11:26:56 +08:00
@roychan 应该是这个问题了,所以这是个错误的写法,丢人了😂
IanPeverell
2018-04-01 11:31:01 +08:00
>>> ll = [1]
>>> lll = ll.append(1)
>>> lll
>>> ll
[1, 1]
NoAnyLove
2018-04-01 12:08:34 +08:00
愚人节?
laoyur
2018-04-01 12:23:41 +08:00
所以内存一直增加是什么鬼👻?
Zzde
2018-04-01 12:27:04 +08:00
愚人节
congeec
2018-04-01 12:31:02 +08:00
@roychan Python 函数默认返回 None,所有函数都有返回值
limerence12138
2018-04-01 12:37:16 +08:00
@laoyur 内存真的一直增加了,我猜是 pycharm 的问题了
jmc891205
2018-04-01 13:17:56 +08:00
这是我讨厌 ide 的原因之一。。。他们为了各种各样“方便”的功能总是在我写 code 的时候背着我做些我不知道的事情。。。
roychan
2018-04-01 13:36:57 +08:00
@congeec 我表述有误
Mitt
2018-04-01 15:18:19 +08:00
@jmc891205 这跟 ide 似乎无关把 ide 也只是调用了官方的调试器等功能 本身并没有做什么 hack 操作
superhan
2018-04-01 15:31:10 +08:00
ide 问题 前几天我也碰到了 和代码也没什么关系
careofzm
2018-04-01 15:48:32 +08:00
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
以上是文档,我们摘取 append 部分
```
list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
```
首先: 我们使用[].append 是意味着空的 list 的尾部添加 value,这个是其功能。
然后:文档中给出了样例的,使用[].append[x] 其实等于使用 a[len(a):]=[x],这个操作已经是一个语句了, 但是似乎一个语句包含另一个语句的时候, 还是合理的
```
In [14]: l = l[len(l):]=[x]

In [15]: l
Out[15]: [1, 1]
```
这个现象显然不能解释,于是又看了源码(这个我以后探究一下是什么问题)
```
class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
"""
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -> None -- append object to end """
pass

def clear(self): # real signature unknown; restored from __doc__
""" L.clear() -> None -- remove all items from L """
pass

def copy(self): # real signature unknown; restored from __doc__
""" L.copy() -> list -- a shallow copy of L """
return []

def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0

def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass

def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0

```
源码的中的注释很明显解释了这个问题,就是说在 list 对象中 append, extend, clear 这些方法都是在逻辑完成之后, 实际返回的都是 None。
rrfeng
2018-04-01 16:14:22 +08:00
lz 是写 js 的吧

我被反向 #想当然# 过...
ipwx
2018-04-01 16:43:00 +08:00
你可以给 PyCharm 提 issue 了。
jmc891205
2018-04-01 17:39:49 +08:00
@Mitt ide 不做额外的事情那怎么提供什么自动补全、跨文件查找等等这些功能。。。就像楼主的问题 不就是 PyCharm 更新 index 的功能有问题吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/443315

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX