class MyClass:
def __init__(self, a):
self.a = a
def append(some_list):
some_list.append(["d"])
foo =[["a"],["b"],["c"]]
bar = foo
my_class = MyClass(foo)
append(bar)
for item in bar:
item[0] += "x"
letters = item[0]
letters += "z"
print (my_class.a)
Yields the output
[['ax'], ['bx'], ['cx'], ['dx']]
There’s quite a bit going on in the example and I feel pretty good about all of it except for I’d expect “z” to be tagged onto the strings as well but it’s not.
Can someone explain why it makes sense “z” is not included on the strings?
I thought that the indexing would return “the container” with the string and then appending the “z” would alter the stored string. Apparently a distinct “container” is made but I don’t understand how or why.
(If appending to the string makes a new string, I don’t know why the same behavior would happen with integers as well which I tested... Would using floats have a different result?)