With old Ziko Python's in-depth variables and reference objects

Source: Internet
Author: User
In the "Forever powerful function" that talk, Lao Qi I have briefed the crossing on the variables, and then we have been using variables, each time the use of variables, there is an action, that is, assign value. This talk about the two things again, is to let crossing on the variables and assignments have a knowledge of and know the reason why. Of course, in the end can not achieve this goal, mainly to see if I say that the plain understandable. If you do not understand, it means that I said is not good enough, you can contact me, I will be at your service.

Variables and objects

In the Learning Python book, the author is very clear about the relationship between variables, objects, and references. I was inspired by him to a great extent. Thanks to the author, Mr. Mark Lutz, for his masterpiece.

Apply one of the ideas in learning Python: Variable no type, object has type

In Python, if you want to use a variable, you don't need to declare it in advance, you just need to assign a value to the variable when you use it. In particular, it is emphasized that it is necessary to assign a value to a variable as long as it is used.

So it's not going to work like this.
Copy the Code code as follows:


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

Repeated reminders: Be sure to pay attention to reading the wrong information. If a variable is written in a light, but not assigned, then Python does not think the variable is defined. Assignment, not only to a non-null value, but also to a null value, as below, is allowed
Copy the Code code as follows:


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

In the previous narration, I put forward an analogy, that is, the variable through a line, connected to the object (in particular, it may be a int/list, etc.), this analogy is accepted by many people, is my old Qi 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. Too serious, not understanding, understand my analogy. A variable is something that exists in a system that has the ability to connect to an object with a line and it can fish.

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

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

See the above figure, it is more vivid expression of the relationship between the variable and the object. So strictly, only the objects that are placed in the memory space (that is, the data) have types, and the variables are of no type. So if you have not fully understood, then play another metaphor: variables are like fishing people, the lake as if the memory, there are many fish, there are all kinds of fish, they are objects. The task of a fisherman (variable) is to connect himself and the fish through a fish line in some way (fish lure). Then, the fish is a type, there are silver carp, Crucian carp, octopus (Octopus also ran to the lake, is it a freshwater fish?) Oh, it's so lame, don't take it seriously), fishing people (variables) without this type, he catches different types of fish.

This metaphor sucks. Make it right. Crossing have a good metaphor, don't forget to share.

Can the same variable point to two objects at a time? Absolutely not seethe. What if it does?
Copy the Code code as follows:


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

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

Found an illustration on the internet, very good, quote come (source: http://www.linuxidc.com/Linux/2012-09/69523.htm)
Copy the Code code as follows:


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

As shown in the following:

Then, it's done again:
Copy the Code code as follows:


>>> a = "Hello"

As shown in the following:

The original 100 in memory was collected as garbage. Moreover, this collection process is done automatically by Python, without our worries.

So, how does Python do garbage collection? On the Quora also someone asked this question, I see that answer is very good, make a link, have a sexual interest 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 one step further.
Copy the Code code as follows:


>>> L1 = [+]
>>> L2 = L1

In this operation, L1 and L2 two variables, referring to an object, are [three-by-three]. Why is it? If the 1,2,3],l2 reference object is modified by L1, the argument is confirmed.
Copy the Code code as follows:


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

Another way:
Copy the Code code as follows:


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

L1 and L2 seem to point to the same object [All-in-one], actually, in memory, it's two things, unrelated. Just the same on the content. It was like two fish in the water, and two people caught it, not the same one. Therefore, when the reference object is modified by L1, the L2 does not change.

Further testing is possible:
Copy the Code code as follows:


>>> L1
[1, 2, 3]
>>> L2
[1, 2, 3]
>>> L1 = = L2 #两个相等 refers to the same content
True
>>> L1 is L2 #is to compare two reference 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, which have copy functions, are the objects obtained by this function, a new one or a reference to the same object? Crossing can also do a similar experiment above, you know. Like what:
Copy the 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 '}

However, crossing is also careful, Python does not always follow the previous way of cards, such as small numbers when
Copy the Code code as follows:


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

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

is the assignment simply an equal sign? From above, the function of = is to have the variable pointer point to an object. But you can go a little deeper. 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.