I am a bit confused about, changing (mutating or appending) variables from within functions.
For reference, i found this question about functions that mutate the argument. which describes doing just that on an array:
def add_thing(a):
a.append(2)
my_a = [1]
append_two(my_a)
my_a
>>> [1,2]
and we get the same results using a += [2]
However, if we try the same thing with a string, or an integer:
def add_world (s):
s += " world"
my_str = "hello"
add_world(my_str)
my_str
>>> "hello"
It doesn't change, and the same goes for integers, like:
def add_one(i):
i += 1
x = 1
add_one(x)
x
>>> 1
My question is:
How should I recognize which objects I can mutate in a function like an array, and which ones I need to directly assign?
why is the += operator not working as I'd expect? I was fairly sure that it was short hand for my_var = my_var + thing, which aught to work just fine within a function.