Calling the pit--------a colleague function throws a Python value reference and a reference to a parameter

Source: Internet
Author: User

Call a colleague's function, pass in the goods_list, get the product information, and then match the product information with the Goods_list information to merge.

But the colleague returned the data and changed the parameters I passed in, Goods_list. Equivalent to a parameter reference, that is, changing data at the same address in memory,

Overwrite the parameter goods_list I passed in. Cause my parameters to not match.

This is hereby recorded:

Let's look at an example.

T_list = [1, 2]
T_dict = {"A": "Haha", "B": "Hehe"}
T_STR = "123"


def test_list (param):
Param[0] = 3
return param

def test_dict (param):
Param["a"] = "Lala"
return param

def test_string (param):
param = "456"
return param

If __name__== "__main__":
Print "==test list=%s"%test_list (t_list)
Print "==test list=%s"%t_list
Print "################################"
Print "==test dict=%s"%test_dict (t_dict)
Print "==test dict=%s"%t_dict
Print "################################"
Print "==test str=%s"%test_string (T_STR)
Print "==test str=%s"%t_str

The return data is as follows

return value ==test list=[3, 2]
Original Value ==test list=[3, 2]
################################
return value ==test dict={' a ': ' Lala ', ' B ': ' Hehe '}
Original value ==test dict={' a ': ' Lala ', ' B ': ' Hehe '}
################################
return value ==test str=456
Original Value ==test str=123

Unlike other languages, Python does not allow programmers to choose whether to pass a value or pass a reference when passing parameters. Python parameter passing is definitely a way to "pass an object reference". In fact, this approach is equivalent to a synthesis of value-passing and 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".

Calling the pit--------a colleague function throws a Python value reference and a reference to a parameter

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.