Quick actions

cmd+k|ctrl+k

Navigation

Languages

Python - Pass by Assignment

Snippet info

Language

Python

Visibility

public

Author

apizzimenti

Created

2016-03-19T05:41:42Z

Updated

2016-03-20T23:31:23Z

'''
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
INFO