1

I am new in python, and I just find something strange:

>>> test="acdefg"
>>> test.replace('a','h')
'hcdefg'
>>> test
'acdefg'
>>> test=[1,2,3]
>>> test.reverse()
>>> test
[3, 2, 1]

As you can see in the code, in the first time, variable "test" is a string, when I call method "replace", the value of "test" doesn't change, the second time is is a list, and the list changed after I called the method reverse().

Why was that? Is it because of something different between the methods or something different between the objects or something else?

Statham
  • 4,000
  • 2
  • 32
  • 45
  • Python strings are immutable. Lists are not. – OneCricketeer Oct 29 '16 at 15:26
  • So it's about the property of the objects or about the property of the methods or both? – Statham Oct 29 '16 at 15:43
  • 1
    @Statham Did you read my answer? It answers that. – idjaw Oct 29 '16 at 15:44
  • 2
    The property of the objects determines the implementation of the method. It's just one of those things that typically requires reading the documentation or trying out small examples to determine behavior – OneCricketeer Oct 29 '16 at 15:45
  • @idjaw Yes I have read it, and I now know that. The "immutable" was just a little strange to me,because I learn C first, in which anything can be changed through a simple assign. But now I know definitely that I should not presume Python has the same property as C :p – Statham Oct 29 '16 at 15:56

2 Answers2

1

It depends entirely on the implementation of the method. Some methods modify the objects they're called on, some do not.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Then when I use a new method, how can I know whether the method does modify the objects or not? By reading the source code? It seems a little hard for a new learner :( – Statham Oct 29 '16 at 15:25
  • 2
    @Statham either that, or more simply, read the documentation. – Mureinik Oct 29 '16 at 15:26
  • Someone commented the question saying that "Python strings are immutable. Lists are not. ", I think he is talking about the property of the objects, and you said that it totally depends on the implementation of the method. Can I say that you are talking about the same thing in different perspective? – Statham Oct 29 '16 at 15:48
  • Think of it this way. The string method will usually return something because the string is immutable, so you can't perform in-place operations. List operations usually will return None because they can be done in place since they are mutable. – idjaw Oct 29 '16 at 16:01
  • @idjaw That's exactly where I got confused before ! ! ! But now I understand why. Btw, the String is immutable so when we want to change the value of a String variable, we can not **"really change"** it, we just assign another value to it, right? – Statham Oct 29 '16 at 16:09
  • 1
    @Statham That is correct. Here is another answer that will provide more details as well: http://stackoverflow.com/a/9098038/1832539 – idjaw Oct 29 '16 at 16:12
1

Strings are immutable. So you aren't actually changing test. You are actually getting the return of the replace string method. To use this modified string, you have to create a new string, or simply replace the existing string with the new value.

>>> some_string = "abcd"
>>> new_string = some_string.replace('a', 'x')
>>> new_string
xbcd

>>> some_string = "abcd"
>>> some_string = some_string.replace('a', 'x')
>>> some_string
xbcd

The second example, the list is mutable, and you are performing an in place manipulation of the list. If you actually do this:

res = your_list.reverse()

res will actually be None, because it doesn't return anything, it actually does it in place, which is why test list will hold the new manipulation you performed.

Read this on immutable vs mutable types in Python.

Also, refer to the documentation here on the Data Model to further your understanding as well.

Community
  • 1
  • 1
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • I just remember something in Java, when I want to modify a String I kept use `a=a.replace(..........)` – Statham Oct 29 '16 at 15:29
  • @Statham Do not try to use one language like it is the other. You have to refer to the documentation and understand how the language you are using actually behaves. How its methods work and how the different data types are treated in the language. It is a big mistake to think that language A should behave exactly like language B. – idjaw Oct 29 '16 at 15:31