Python function parameter pass value or pass reference __ function

Source: Internet
Author: User
Keys = {"Status": True, ' name ': ' AAA '}

def fun1 (keys):
    print ' fun1 '
    print ' keys: ', keys
    print ' keys Address ", ID (keys)
    keys[' status ' = False

def fun2 (keys):
    print ' fun2 '
    print ' keys: ', keys
    print ' Keys address ", ID (keys)
    
fun1 (keys)
fun2 (keys)

#fun1
#keys: {' status ': True, ' name ': ' AAA '}
# Keys address 49248184

#fun2
#keys: {' status ': False, ' name ': ' AAA '}
#keys address 49248184

From the results above, the dictionary passed in a reference, within which the value could be changed.


Look at the string below

str1= ' Hello World '

def fun1 (str1):
    str1= "Ni hao"
    print "fun1 str1:", STR1, "ID:", ID (str1)

print " STR1: ", STR1," ID: ", ID (str1)
fun1 (str1)
print" str1: ", STR1," ID: ", ID (str1)

#str1: Hello World id:39530
#fun1 Str1:ni Hao id:40521600
#str1: Hello World id:39530000


From the above, the string does not change after the function call, but it changes inside the function. So the corresponding tuple, numeric constants, are not going to change.

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.