Python small data base copy pool and shades

A small pool of data

  • == (equal)

  • == official: the contents of both sides of the equal sign is the same judge

    Vernacular: determine whether two people look is not the same

  • is

    Official: The content is not a judgment on both sides

    Vernacular: determine the two men is not a person

  • a = 10000

  • b = 10000

  • print(a == b)

    is the memory address is determined by

    print(id(a),id(b))

    Output, the same memory address

  • Each line of a py file, a function, a module, a class, interactive mode (terminal) under: code block


First block of code, and then a small data pool!
Block content mechanism: Python command to initialize the object when executing the same code block, it checks if the value already exists, if there is, it will be reused. In other words: When you perform the same block, encountered command to initialize the object, he will be initialized with the value of this variable is stored in a dictionary, in the face of new variables will first query records in the dictionary, If you have the same record then it will re-use this value before this dictionary. So you example given, when the file executed (same block) will i1, i2 two variables point to the same object, satisfies the caching mechanism in them there is only one memory, namely: the same id.

Terminal test is a small pool of data cache mechanism:

  ​ 数字: -5 ~ 256 

  ​ 字符串:

  ​             定义时内容(除去中文,特殊符号)长度不限,内容相同,就进行驻留。

  ​          python 3. 6解释器字符串进行乘法时(不能有中文和特殊符号),总长度不能超过20

  ​            python 3. 7解释器字符串进行乘法时(不能有中文和特殊符号),总长度不能超过4096

pycharm caching mechanism is tested in a block of code:

    数字:      -5 ~ 正无穷

  字符串:         

  ​          定义时内容长度不限,内容相同,就进行驻留

  ​         字符串进行乘法时(不能有中文和特殊符号),总长度不能超过20

Second, copy depth

Assignment

lst = [1,2,3]    # 可变数据类型
lst1 = lst
lst.append(4)
print(lst,lst1)      #  lst赋值给了lst1 共用一块内存空间,故lst 添加,lst1也添加
输出结果 [1,2,3,4]  [1,2,3,4]
# 总结:  多个变量指向同一个内存地址,如果这个内存地址的数据是
#       修改时不可变的数据类型时,会新开辟空间(字符串,数字)
#       修改时可变的数据类型时,会在原地进行修改(列表,字典)

Shallow copy

Shallow copy only the memory address of the original records in the list to get a list of newly opened in order to better understand, look at the example of Figure!

lst = [1,2,3,[6,7,8]]
# lst2 = lst[:] # 浅拷贝
lst2 = lst.copy()

Figure orange is the new open space, light blue is numeric, red list type


lst = [1,2,3,[6,7,8]]
lst2 = lst.copy()
lst[1] = "22"

We modified the string "22" is in the list to replace the previous memory address space to open up a new address


lst = [1,2,3,[6,7,8]]
lst1 = lst.copy()
lst[-1].append(9)

Shallow copy Summary: shallow copy only the most open in outer space, where the elements are the same memory address, that is shared, when immutable data types, like int, str, bool add or delete or modify these elements will not influence each other, because they need to open up their own space, and variable data types, do not need to modify or add or delete from the new open space, additions and deletions in their own space, so the original copy of the object changes, the copied object will change.

Deep copy

import deepcopy
lst = [1,2,3,[6,7,8,9]]
lst1 = copy.deepcopy(lst)
lst[-1].append(10)

深拷贝:深拷贝不可变元素共用,若各自修改都需自己开辟空间,不会一同改变,假如lst中1需要删除,他的内存地址会删,但是lst2复制的内存地址还是指向1,不会改变,可变数据类型中里面的元素共用,导致各自改变各自的列表内元素,相互不会受到影响,并且列表空间地址不一样,所以各自更改元素受影响的只是自己列表地址空间中的元素而已,不会受到影响!

Guess you like

Origin www.cnblogs.com/zzsy/p/12216065.html