Python wrong title set

Source: Internet
Author: User

Knowing and wrong can change 1. Set the variable a=2, the variable b=a, and then set the A=3, and then look at B will also be equal to 3
    • The answer resolves:

Variable a=2, b=a, and then a=3, the result of viewing a at this time is 3, but the result of B is still 2, because B=a actually does not point the B variable to a, but the value of B is pointed to the value of a variable point, if a after a change to the 3,python interpreter will open a new memory space and the value Save As 3, then a memory pointer points to 3 of the address, but 2 of the memory address is the same, since B points to the address of 2, so the value of B is still 2

总结:+=对于可变对象(dict,list)来说,以最小动作为基础,是在原处修改的= 另外的是生成了一个新的对象
Knowledge Point: Garbage collection 1. Small integer Object pool

Integers are used extensively in programs, and in order to optimize speed, Python uses a small integer pool of objects to avoid frequent application and destruction of memory space for integers.

Python defines small integers as [-5, 257] These integer objects are set up well in advance and are not garbage collected. In a Python program, all integers that are in this range use the same object.

Similarly, a single letter is the same.

However, when you define 2 identical strings, the reference count is 0, which triggers a garbage collection

2. Large integer Object pool

A new object is created for each large integer.

3. Intern mechanism

A1 = "HelloWorld"
A2 = "HelloWorld"
a3 = "HelloWorld"
a4 = "HelloWorld"
A5 = "HelloWorld"
A6 = "HelloWorld"
A7 = "HelloWorld"
A8 = "HelloWorld"
A9 = "HelloWorld"
Will python create 9 objects? Will it open up 9 "HelloWorld" of memory space in memory? Think about it, if this is the case, we write 10,000 objects, such as a1= "HelloWorld" ... a1000= "HelloWorld", then he did not open up 1000 "HelloWorld" occupied the memory space? If so, does the memory explode? So python has such a mechanism--intern mechanism, so that he only occupies a "HelloWorld" occupies the memory space. By reference counting to maintain when to release.

Summary: Garbage collection
    • Small integer [-5,257) Common object, Resident memory
    • Single character shared object, resident memory
    • Single word, non-modifiable, default turn on intern mechanism, common object, reference count is 0, destroy word garbage collection
    • string (containing spaces), non-modifiable, no intern mechanism, no shared object, reference count 0, destroy
    • Large integers do not share memory, reference count is 0, destroy large integer garbage collection
    • numeric types and string types are immutable in Python, which means that you cannot modify the value of this object, and each time you modify a variable, you are actually creating a new object that is not mutable

Python wrong title set

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.