Why are Dictionaries passed as a reference to classes?
I used to think that when I pass variables to a class, they are copied.
class MyClass:
def __init__(self, arg=None):
self.arg = arg
a = 'a'
m1 = MyClass(arg=a)
a = 'b'
print(m1.arg) # Prints "a"
After using Python for a very long time I just figured out that when I pass a Dictionary, it is not copied, but referenced. So changing the variable afterwards, changes the classes variable. That was very unexpected.
d = {'a': 'a'}
m2 = MyClass(arg=d)
d['a'] = 'b'
print(m2.arg) # Prints "{'a': 'b'} ... why suddenly 'b'?"
Does this happen only to dictionaries, or are there other types that are referenced? Why is it different anyway?