Python parameter passing (value & pass-through reference)

Source: Internet
Author: User

# test parameter is a pass or pass reference
def test (ARG):
Print ("Test before")
Print (ID (ARG))
ARG[1]=30 # Testing Mutable objects
# arg[3][2] = 16 # test Nested types
# arg = List (range (5))
Print ("Test after")
Print (ID (ARG))
return arg
Change_loc = 1
if Change_loc = = 1:
# Passing in mutable objects
My_list = List (range (5))
Print (my_list)
Print (ID (my_list))
Test (My_list)
Print (my_list)
Print (ID (my_list))
Else
# Incoming immutable objects (nested lists in tuples)
My_tuple = (3,5, "abc", my_list) # List (range (6) cannot write directly in tuple create list can be replaced by created list object
Print (my_tuple[3][2]) # to test an element of a list in a modified tuple
Print (ID (my_tuple)) # Incoming pre-tuple reference
Print (ID (my_tuple[3)) # Incoming list reference in the preceding tuple
Test (My_tuple)
Print (my_tuple[3][2]) # to test an element of a list in a modified tuple
Print (ID (my_tuple)) # Incoming tuple reference
Print (ID (my_tuple[3)) # Incoming list reference in tuple
# The Python parameter is passed in the way of "object reference". This approach is equivalent to a synthesis of the value of the pass and the reference.
# If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference".
# If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value".

Summary: If you pass an immutable object when passing the value of a function, you cannot modify the original value, and you can change the original value by passing the variable variable. If a nested data type is passed (for example: ("You", [' Jack ', ' Rose ']) The tuple contains a list or [{' Tom ', ' Jim ', ' Mary '},{' Jack ', ' Rose '} ' list containing the collection, etc.) the modification limit is outside and within, Directly connected to the two layers of the outer limit of the inner layer, not directly connected to no direct impact. Take the tuple inclusion list as an example: tuples cannot be modified directly, so references to individual data within tuples cannot be modified, and lists support modifications, so you can modify the data in a tuple's list, which does not affect the immutable nature of the tuple itself, because the reference to the list does not change, and the tuple has no direct influence on the data in its list. Other nested structure analysis is similar!!!

Test results:

Python parameter passing (value & pass-through reference)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.