With the old Ziko python depth variables and reference objects _python

Source: Internet
Author: User
Tags garbage collection in python

In the "Forever powerful function" of the talk, Old Qi I have to reader to brief the variables, and then we have been using variables, each use of variables, have an operation, is the assignment. Once again, the two things we have to say about this is to let reader have a knowledge of the variables and the values of the assignment. Of course, finally can achieve this goal, mainly to see if I am speaking of easy to understand. If you do not understand, it means that I said is not good enough to contact me, I can help you.

Variables and objects

In the book "Learning Python," the author explains the relationship between variables, objects, and references. I am here to a great extent inspired by him. Thank the author, Mark Lutz, for his magnum opus.

Application of the idea in learning Python: variables have no type, objects have types

In Python, if you want to use a variable, you don't need to declare it in advance, just assign a value to the variable when you use it. It is particularly emphasized here that the variable should be assigned a value as long as it is used.

So it's not going to work like this.

Copy Code code as follows:

>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Nameerror:name ' x ' is not defined

Repeatedly remind: must pay attention to the newspaper wrong information. If you write a variable in light, without assigning a value, Python does not think the variable is defined. Assignment, not only to a non-empty value, but also to a null value, such as the following, are allowed

Copy Code code as follows:

>>> x = 3
>>> LST = []
>>> Word = ""
>>> my_dict = {}

In the previous story, I put forward an analogy, that is, the variable through a thread, attached to the object (specifically may be a int/list, etc.), this analogy was accepted by many people, is my old Qi's first. So, if you want to describe in a strict language, a variable can be understood as an element of a system table that has a namespace that points to an object. It's so serious, it's hard to understand, so understand my analogy. A variable is one of the things in the system that has the ability to connect to an object with a thread that can fish.

What about the object? Unfold the imagination. In the machine's memory, the system allocates a space, where the so-called objects, sometimes put numbers, sometimes put strings. If you put a number, it's the int type, and if you put the string, it's the str type.

The next thing is that the variables mentioned above use their own capabilities to connect objects to themselves (pointers connect to object space), which is the reference. When the reference completes, the assignment is implemented.

See the above diagram, from the diagram is more vivid representation of the relationship between the variables and objects. So, strictly, only objects that are placed in memory space (that is, data) are of type, and variables are of no type. So if you haven't fully understood it, then make another analogy: the variable is like a fish, the lake is like memory, there are a lot of fish, there are all kinds of fish, they are objects. The task of a fisherman (a variable) is to connect himself and the fish through a line of fish in some way (lure by the fish). Well, there are types of fish, there are silver carp, Crucian carp, Octopus (Cutlass also ran to the lake, is it fresh water octopus?) Oh, it is so nonsense, do not seriously, fishing people (variables) do not have this type, he caught different types of fish.

That's a bad metaphor. Make up your understanding. Reader have a good analogy, don't forget to share.

Can the same variable point to two objects at the same time? Absolutely not on both sides. What if it does?

Copy Code code as follows:

>>> x = 4
>>> x = 5
>>> x
5

The variable x points first to object 4 and then to Object 5, which automatically contacts the first object 4 when the latter is released. Look at x again, the object of reference is 5. So what about 4? Once there is no variable to quote it, it becomes a wandering ghost. Python is so stingy that it definitely does not allow ghosts to exist in memory. All these things are considered rubbish, and in the garbage, Python has an automatic recovery mechanism.

Found a picture on the internet, very good, quote come (source: http://www.linuxidc.com/Linux/2012-09/69523.htm)

Copy Code code as follows:

>>> A = #完成了变量a对内存空间中的对象100的引用

As shown in the following illustration:

Then, again, the operation:

Copy Code code as follows:

>>> a = "Hello"

As shown in the following illustration:

The original memory of the 100 as garbage was collected. Also, this collection process is automatically done by Python, without us worrying.

So how does Python do garbage collection? On the Quora also someone asked this question, I see that answer is very wonderful, do a link, have a sexual interest to read it. Python (programming language): How does garbage collection in Python work?

IS and = = effect

The principle of the above process is clear, the following can be a step further.

Copy Code code as follows:

>>> L1 = [1,2,3]
>>> L2 = L1

In this operation, L1 and L2 two variables that refer to an object, all of which are [1,2,3]. Why is that? If you modify the [1,2,3],l2 Reference object by L1, it confirms this view.

Copy Code code as follows:

>>> l1[0] = #把对象变为 [99,2,3]
>>> L1 #变了
[99, 2, 3]
>>> L2 #真的变了吔
[99, 2, 3]

Another way:

Copy Code code as follows:

>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1[0] = 99
>>> L1
[99, 2, 3]
>>> L2
[1, 2, 3]

L1 and L2 seem to point to the same object [1,2,3], in fact, in memory, this is two things, unrelated. Just as in the content. It's like two fish in the water, two of them are caught, not the same one. Therefore, when the reference object is modified by L1, the L2 does not change.

To further test this:

Copy Code code as follows:

>>> L1
[1, 2, 3]
>>> L2
[1, 2, 3]
>>> L1 = = L2 #两个相等, refers to the same content
True
>>> L1 is L2 #is compare two referenced objects in memory address is not the same
False #前面的检验已经说明, this is two Dongdong

>>> L3 = L1 #顺便看看如果这样, L3 and L1 apply the same object
>>> L3
[1, 2, 3]
>>> L3 = = L1
True
>>> L3 is L1 #is的结果是True
True

Some objects, with the copy function, get the object from this function, is it a new one or a reference to the same object? Reader can also do a similar experiment, you know. Like what:

Copy Code code as follows:

>>> L1
[1, 2, 3]
>>> L2 = l1[:]
>>> L2
[1, 2, 3]
>>> L1[0] = 22
>>> L1
[22, 2, 3]
>>> L2
[1, 2, 3]

>>> adict = {"Name": "Qiwsir", "Web": "Qiwsir.github.io"}
>>> bdict = Adict.copy ()
>>> bdict
{' Web ': ' Qiwsir.github.io ', ' name ': ' Qiwsir '}
>>> adict["Email" = "qiwsir@gmail.com"
>>> adict
{' Web ': ' Qiwsir.github.io ', ' name ': ' Qiwsir ', ' email ': ' qiwsir@gmail.com '}
>>> bdict
{' Web ': ' Qiwsir.github.io ', ' name ': ' Qiwsir '}

But, reader, there's a bit of caution, Python doesn't always play the cards the way it said, like small numbers.

Copy Code code as follows:

>>> x = 2
>>> y = 2
>>> x is y
True
>>> x = 200000
>>> y = 200000
>>> x is y #什么道理呀, when small numbers are used in the cache.
False

>>> x = ' Hello '
>>> y = ' Hello '
>>> x is y
True
>>> x = "What is and you name?"
>>> y = "What is and you name?"
>>> x is y #不光小的数字, short strings are also
False

is the assignment simply an equal sign? From above, the function of = is to let the variable pointer point to an object. However, it can be further deepened. Let's see.

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.