0 Fundamentals python-17.2 parameters and shared references

Source: Internet
Author: User

In this chapter we will discuss the parameters and shared references

The delivery of the parameters described in the previous section is achieved by automatically assigning the object to the local variable name, essentially copying the reference, passing the object

1. We take the following as an example of passing immutable objects:


as you can see from the code, X is assigned a reference to 1 for this object, just a reference to the 1 assignment to B is copied to x, so the ID of B and X are the same, referencing the same memory address

And when x is changed to 2, just recreate an object and assign the reference to x, so the ID of x is changed, that is, the referenced object has changed.


2. Let's take a look at variable objects for example:


We use the above code, and then add a modifiable object, from the result can be seen, non-modifiable object satisfies the 1th conclusion, but, can modify the object is not the same, throughout the operation, A and Y refers to the object is not changed, just modify the object inside one of the values, Here's a picture to illustrate


Because parameters are passed by assignment, the parameter names in the function can be called when the shared object is implemented by changing. Therefore, changes in the in-place of variable object parameters in a function can affect the caller.


3. Avoid changes to mutable objects

>>> b=1>>> a=[1,2,3]>>> def test (x, y):p rint (x) print (ID (x)) X=2print (x) print (ID (x)) print ( Y) print (id (y)) y[1]=6print (y) print (id (y)) >>> Test (b,a[:]) #改动的地方14977340962497734112 [1, 2, 3]37588624[1, 6, 3]37588624>>> a[1, 2, 3]


We just need to make a copy of the list to prevent the list from changing after the operation.

Or we have copied it in the function, and we don't need to operate outside of the function.



Summary: This section provides a brief description of the parameters and shared references, as well as the problems and solutions that occur when the parameter is a mutable object.


This chapter is here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Fundamentals python-17.2 parameters and shared references

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.