The memory transmission mechanism in
Python combines features from both pass-by-value and pass-by-reference into what is best described as “pass-by-object-reference” or “pass-by-assignment”. Functions accept argument references only instead of receiving the original objects when they are invoked. The ability to modify objects by functions depends on the object type since mutable elements allow modifications while immutable elements do not.
A mutable object passed to a function allows changes made within the function to modify both the original object located outside the function boundaries. Assigning a new object through the function parameter has no effect on the initial object stored outside the function.
Modification attempts on immutable objects within a function do not impact the original object outside the function because the function instead binds a new object reference to its local scope.
Understandably this operational style causes ambiguity when developers expect Java-like value passing or C++-like reference copying behaviors. Inside
Python functions users can receive object references but the function prevents modifications of these references by external code.
Code Example
def modify_list(lst):
lst.append(4) # modifies the original list
def modify_number(num):
num += 10 # creates a new number object, original remains unchanged
my_list = [1, 2, 3]
my_num = 5
modify_list(my_list)
modify_number(my_num)
print(my_list) # Output: [1, 2, 3, 4]
print(my_num) # Output: 5
The example shows the list becomes modified since it is mutable yet the integer stays unchanged because it is immutable.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The memory transmission mechanism in Python combines features from both pass-by-value and pass-by-reference into what is best described as “pass-by-object-reference” or “pass-by-assignment”. Functions accept argument references only instead of receiving the original objects when they are invoked. The ability to modify objects by functions depends on the object type since mutable elements allow modifications while immutable elements do not.
A mutable object passed to a function allows changes made within the function to modify both the original object located outside the function boundaries. Assigning a new object through the function parameter has no effect on the initial object stored outside the function.
Modification attempts on immutable objects within a function do not impact the original object outside the function because the function instead binds a new object reference to its local scope.
Understandably this operational style causes ambiguity when developers expect Java-like value passing or C++-like reference copying behaviors. Inside Python functions users can receive object references but the function prevents modifications of these references by external code.
Code Example
The example shows the list becomes modified since it is mutable yet the integer stays unchanged because it is immutable.