Python - Pass by Assignment

Run Settings
LanguagePython
Language Version
Run Command
''' Python is an interesting language in that it passes mutable types by reference and immutable types by value. Variables that are assigned a mutable object (lists, user-defined classes) contain a pointer (reference) to that object. Any changes made to the variable are reflected in the original object and all pointers pointing to the object. Variables that contain an immutable-type object can have their value changed, but those changes are not reflected in the original object. ''' ''' Mutable > Since a is mutable, b and c simply contain pointers to a. ''' class a: def __init__(self, n): self.n = n b = a(1) c = b b.n = 2 c.n = 3 print(b.n == c.n) # should return True ''' Immutable > Since int is an immutable type, changing e to 2 would change the value of 1 > itself, which would break everything. e and d contain the value 1, and each > variable can be remapped to contain a different value. ''' d = 1 e = d e = 2 print(d == e) # should return False
Editor Settings
Theme
Key bindings
Full width
Lines