-2

A similar (but not the same) question is asked here; this question is about why a casted object's methods don't alter the underlying values and return them to their original variable type.

In Python, you can cast a variable with list(). Why doesn't the following code work as one might expect?

stir = "lolololol"
for i in range(11):
  if i % 2:
    list(stir).insert(i, ":")

I would expect "stir" to be casted a list, the .insert() method called, the original variable's value modified as if it were a list, then returned to the variable type it was before. Instead I get no changes.

Setting the output to a new variable also produces Nonetype:

x = list(stir).insert(0, ":")
print x
Community
  • 1
  • 1
Locane
  • 2,886
  • 2
  • 24
  • 35
  • There's nothing linking the new list to `stir` after it's created. Modifying the list won't automatically modify `stir` or anything else. – user2357112 Mar 11 '17 at 00:31
  • 4
    It might help to stop thinking in terms of "casting" and start thinking in terms of objects. You're creating a new list object with `list(stir)`, not treating the original string as a list. – user2357112 Mar 11 '17 at 00:33
  • Because `list` isn't really a "cast" (a term which I don't think you should use outside of C-like languages). But in any event, `list(someting)` makes a **new list** out of something, which you then modify in-place, but don't capture, so it gets garbage collected. – juanpa.arrivillaga Mar 11 '17 at 00:47
  • Briefly: the `insert` method works in-place. `list` does not. – TigerhawkT3 Mar 11 '17 at 01:01
  • @TigerhawkT3, thanks for your input. This question is not a duplicate of the one you linked to, however, as it's asking about calling the method of the object after casting and why the original variable isn't modified in place and returned to its original type. The question that you linked to as a duplicate only asks why "list(x)" doesn't turn x in to a list indefinitely. – Locane Mar 14 '17 at 20:16
  • It is a duplicate. You are using a function that returns a new object and not saving anything anywhere. Regardless of what methods you chain (except for horrible namespace hacks), it still won't be saved. – TigerhawkT3 Mar 14 '17 at 20:33
  • @TigerhawkT3 I respectfully disagree. The answer may be similar, but the question of "why doesn't an object method that modifies in place modify the original values of a cast variable" is different from "why doesn't casting change the object type". Hopefully you can see how those two things are different. – Locane Mar 15 '17 at 20:16

1 Answers1

1

When calling list(stir) you are creating a new list object of the stir object. And with .insert(0, ":") you add to the new list object, not to the original object.

Also calling list(stir).insert(0, ":") is a function call and since insert() doesn't have a return value, x will be declared as None.

Jonatan
  • 1,182
  • 8
  • 20
  • 1
    I'm not sure "clone" is the appropriate term here. A clone implies something that is the same type. And it doesn't return a `void`, it returns the `None` object. – juanpa.arrivillaga Mar 11 '17 at 00:48
  • @juanpa.arrivillaga Thanks, it's getting a bit late and wasn't paying too much attention. – Jonatan Mar 11 '17 at 00:56