[].append(x) behaviour

后端 未结 4 702
北荒
北荒 2021-01-23 07:21

This executes as I\'d expect:

>>>x=[]
>>>x.append(3)
>>>x
[3]

Why does the following return None?

&g         


        
相关标签:
4条回答
  • 2021-01-23 07:58

    because list.append changes the list itself and returns None ;)

    You can use help to see the docstring of a function or method:

    In [11]: help(list.append)
    Help on method_descriptor:
    
    append(...)
        L.append(object) -- append object to end
    

    EDIT:

    This is explained in docs of python3:

    Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.

    0 讨论(0)
  • 2021-01-23 08:07

    If the question is "why is x None?", then it's because list.append returns None as others say.

    If the question is "why was append designed to return None instead of self?" then ultimately it is because of Guido van Rossum's decision, which he explains here as it applies to a related case:

    https://mail.python.org/pipermail/python-dev/2003-October/038855.html

    I'd like to explain once more why I'm so adamant that sort() shouldn't return 'self'.

    This comes from a coding style (popular in various other languages, I believe especially Lisp revels in it) where a series of side effects on a single object can be chained like this:

    x.compress().chop(y).sort(z)

    which would be the same as

    x.compress()
    x.chop(y)
    x.sort(z)

    I find the chaining form a threat to readability;

    In short, he doesn't like methods that return self because he doesn't like method chaining. To prevent you from chaining methods like x.append(3).append(4).append(5) he returns None from append.

    I speculate that this could perhaps be considered a specific case of the more general principle that distinguishes between:

    • pure functions, that have no side-effects and return values
    • procedures, that have side-effects and do not return values

    Of course the Python language doesn't make any such distinction, and the Python libraries do not apply the general principle. For example list.pop() has a side-effect and returns a value, but since the value it returns isn't (necessarily) self it doesn't violate GvR's more specific rule.

    0 讨论(0)
  • 2021-01-23 08:10

    The append method of lists returns None. It only modifies the list it is bounded to. Same will happen with:

    x = {}.update(a=3)
    
    0 讨论(0)
  • 2021-01-23 08:21

    The method list.append() changes the list inplace, and returns no result (so returns None if you prefer)

    Many methods of list work on the list inplace, so you doesn't need to reassign a new list to override the old one.

    >>> lst = []
    >>> id(lst)
    4294245644L
    >>> lst.append(1)
    >>> id(lst)
    4294245644L   # <-- same object, doesn't change.
    

    With [].append(1), you're adding 1 to a list freshly created, and you have no reference on this one. So once the append is done, you have lost the list (and will be collected by the garbage collector).
    By the way, fun fact, to make sense to my answer:

    >>> id([].append(1))
    1852276280
    >>> id(None)
    1852276280
    
    0 讨论(0)
提交回复
热议问题