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

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

  •  
  •   limerence12138 · 2018-04-01 11:14:09 +08:00 · 4363 次点击
    这是一个创建于 2210 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    19 条回复    2018-04-01 17:39:49 +08:00
    limerence12138
        1
    limerence12138  
    OP
       2018-04-01 11:15:56 +08:00
    哪位兄弟来简单解释一下原因啊,是从对象,引用,这方面分析吗,或者是文档
    roychan
        2
    roychan  
       2018-04-01 11:18:39 +08:00   ❤️ 6
    append 本身就不返回任何值
    picture2200
        3
    picture2200  
       2018-04-01 11:19:32 +08:00 via Android
    会不会是 pycharm 的问题
    wellsc
        4
    wellsc  
       2018-04-01 11:21:31 +08:00 via iPhone
    LL = []
    LL.append(1)
    print(LL)
    limerence12138
        5
    limerence12138  
    OP
       2018-04-01 11:26:56 +08:00 via Android
    @roychan 应该是这个问题了,所以这是个错误的写法,丢人了😂
    IanPeverell
        6
    IanPeverell  
       2018-04-01 11:31:01 +08:00
    >>> ll = [1]
    >>> lll = ll.append(1)
    >>> lll
    >>> ll
    [1, 1]
    NoAnyLove
        7
    NoAnyLove  
       2018-04-01 12:08:34 +08:00
    愚人节?
    laoyur
        8
    laoyur  
       2018-04-01 12:23:41 +08:00
    所以内存一直增加是什么鬼👻?
    Zzde
        9
    Zzde  
       2018-04-01 12:27:04 +08:00 via iPhone
    愚人节
    congeec
        10
    congeec  
       2018-04-01 12:31:02 +08:00
    @roychan Python 函数默认返回 None,所有函数都有返回值
    limerence12138
        11
    limerence12138  
    OP
       2018-04-01 12:37:16 +08:00 via Android
    @laoyur 内存真的一直增加了,我猜是 pycharm 的问题了
    jmc891205
        12
    jmc891205  
       2018-04-01 13:17:56 +08:00 via iPhone
    这是我讨厌 ide 的原因之一。。。他们为了各种各样“方便”的功能总是在我写 code 的时候背着我做些我不知道的事情。。。
    roychan
        13
    roychan  
       2018-04-01 13:36:57 +08:00
    @congeec 我表述有误
    Mitt
        14
    Mitt  
       2018-04-01 15:18:19 +08:00 via iPhone
    @jmc891205 这跟 ide 似乎无关把 ide 也只是调用了官方的调试器等功能 本身并没有做什么 hack 操作
    superhan
        15
    superhan  
       2018-04-01 15:31:10 +08:00 via Android
    ide 问题 前几天我也碰到了 和代码也没什么关系
    careofzm
        16
    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
        17
    rrfeng  
       2018-04-01 16:14:22 +08:00 via Android
    lz 是写 js 的吧

    我被反向 #想当然# 过...
    ipwx
        18
    ipwx  
       2018-04-01 16:43:00 +08:00
    你可以给 PyCharm 提 issue 了。
    jmc891205
        19
    jmc891205  
       2018-04-01 17:39:49 +08:00
    @Mitt ide 不做额外的事情那怎么提供什么自动补全、跨文件查找等等这些功能。。。就像楼主的问题 不就是 PyCharm 更新 index 的功能有问题吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5245 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:02 · PVG 15:02 · LAX 00:02 · JFK 03:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.