0

I have two sets of code for taking the list A=[1,2,3] and changing A into [1,2,3,4].

Here is the first code, followed by what it prints.

A=[1,2,3]
A.append(4)
print(type(A))
print(A)

<class 'list'>
[1, 2, 3, 4]

This first code works.

Here is the second code, followed by what it prints.

A=[1,2,3]
A=A.append(4)
print(type(A))
print(A)

<class 'NoneType'>
None

This second code returns None.

  • What is the explanation for this and why the second code is "wrong"?
  • It seems that x=f(x,y) is fine if f is a function but x=x.method(y) is completely different.

For example, suppose I want to turn 'no no no' into 'yeah yeah yeah':

text='no no no'
text=text.replace('no','yeah')
text

'yeah yeah yeah'

text='no no no'

text.replace('no','yeah')

text

'no no no'

So now the situation is reversed. So you just have to learn which one is correct for each method?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 3
    `.append()` is an in-place operation. It modifies the original list and returns `None`. – StardustGogeta Jul 24 '19 at 15:48
  • I think "in-place" is the thing I'm not really understanding, although I have heard the term before now. – John Smith Jul 24 '19 at 15:49
  • "In-place" operations are basically defined as those which modify the original object calling the method. Contrast this with methods like `.replace()` for strings, which returns a modified `str` object rather than changing the original one. – StardustGogeta Jul 24 '19 at 15:51
  • So if the method was .replace it would be the *second* style of code that was correct, while the first style of code will do nothing? – John Smith Jul 24 '19 at 15:58
  • Yep, that's right. I don't know of any good way to tell if any given method is in-place or not, but strings (which are immutable) do not have in-place methods while lists/dictionaries (which are mutable) do. Strangely enough, `list.sort()` is in-place, but `sorted()` is not. Links like https://stackoverflow.com/questions/4772987/how-are-python-in-place-operator-functions-different-than-the-standard-operator and https://stackoverflow.com/questions/13062423/is-making-in-place-operations-return-the-object-a-bad-idea might help. – StardustGogeta Jul 24 '19 at 16:08
  • @JohnSmith because that method returns `None`. It is as simple as that. there is nothing, in principle, preventing it from returning something else, but it just doesn't. – juanpa.arrivillaga Jul 24 '19 at 16:08
  • https://www.geeksforgeeks.org/inplace-vs-standard-operators-python/ might also be a good source to look at. – StardustGogeta Jul 24 '19 at 16:08
  • @StardustGogeta `str` objects will never have methods that work in-place *because they are immutable*, otherwise, by definition, they wouldn't be immutable. Immutability simply means lacking mutator methods. In any case, the fact that mutator methods *tend* to return `None` is a design choice, not a fundamental aspect of the language. Someone else may have designed an API that returns the object itself, for a fluent interface (not idiomatic in python) – juanpa.arrivillaga Jul 24 '19 at 16:09
  • 3
    Possible duplicate of [Why does append return None in this code?](https://stackoverflow.com/questions/16641119/why-does-append-return-none-in-this-code) – juanpa.arrivillaga Jul 24 '19 at 16:10

1 Answers1

2

When you put A=A.append(4), you are assigning to the variable A the return value of A.append(4), which is None.

The append() function is an in-place function, meaning that it modifies your original list when you call it - it does not return the modified list.

To be more exact, when you execute the line A=A.append(4),

  1. The append(4) function is called, and the value of A is [1, 2, 3, 4]
  2. ...and then the value returned by append(4) - which is none - is immediately assigned to A.

Whatever append() does to your list is discarded, since the line ultimately evaluates to A=None because of this.

This is different than replace(), which does return the new, modified value of the string. To answer your question, yes, you would just have to check the documentation on an individual basis to know the correct usage for each function.

lambdawaff
  • 141
  • 5