python-Variable Object reference

Source: Internet
Author: User

Learn Python first to understand these three relationships, variables, objects, references, types
This should be remembered: types exist on objects, variables are referenced by reference to the object
Variables are created when they are assigned, can refer to objects of any type, and are used to assign values prior to use.

<center>

For example:
": A = 3
In fact, three things are done:

1. Created an object of type int 3
2. Created an object named a
3. Connect the variable name A to object 3 of type int

A variable is actually a pointer to an object's memory space, which is an element of a system table and has a link space to the object.
Objects are allocated pieces of memory and have enough space to represent the values they represent
A reference is an automatically formed pointer from a variable to an object

Remember: Each object has two things, object type and reference counter this one thing determines whether or not to recycle automatically.
Object garbage Collection: Each object maintains a reference counter that records the current number of references to the object, and once this counter is 0, the object's memory space is automatically reclaimed.

Import Sys
Print (Sys.getrefcount (L)) View reference pairs less times

getrefcount(...)getrefcount(object) -> integer    Return the reference count of object.  The count returned is generally    one higher than you might expect, because it includes the (temporary)    reference as an argument to getrefcount().     
Shared references
思考:a =  "3"b = aa = "spam"这样输出b会是多少?          



Variable a refers to the memory space of the spam object, and all valuable objects 3 must be quoted, so who uses it is variable B

A = 3
b = A
A = a + 3

L = [n/A]
L2 = L
L = 24
At this point the L2 remains unchanged if the following statement is written to replace l=24
L[0] = 24
This changes the value of the L2, because the list L refers to the object, L[0] is in place to modify the reference value, L and L2 is a shared reference, so L modified the first object's reference, L2 will also change

Better understanding of shared references

    ==,is     is 是比较实现应用的指针,如果不用变量名,引用相同指针,那么返回true。


First question: A and B are initially shared and referenced, but then B is re-pointing to another object, so it does not affect a
The second question: A and B initially share the reference, but the list is supported in the same place, so the assignment b[0] is in situ to modify the pointer to other places, then a must also change, the difference is that b[0] is in situ modification.
The third question, in fact b=a[:] is to create two list,a and B, that is, create two lists, a, two different list objects with the same value, so b[0] in the same place, does not affect A

python-Variable Object 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.