Python Learning Diary

Source: Internet
Author: User
1. Numerical copy

Import copy# ##### #数字, string ##### #n1 = 123print (ID (n1)) N2 = N1print (id (n2)) # # #浅拷贝 # # # #n2 = copy.copy (n1) print (ID (n2)) # # #深拷贝 # # #n3 = copy.deepcopy (n1) print (ID (n3))

C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe c:/homework/day3/1.py
492322480
492322480
492322480
492322480

Process finished with exit code 0

2. String

Import copy# ##### #数字, string ##### #n1 = "My name is Hellworld" Print (ID (n1)) N2 = N1print (id (n2)) # # #浅拷贝 # # #n2 = copy.copy (n1) PRI NT (ID (n2)) # # #深拷贝 # # #n3 = copy.deepcopy (n1) print (ID (n3))

C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe c:/homework/day3/1.py
5973920
5973920
5973920
5973920

Process finished with exit code 0

Import copy# ##### #数字, string ##### #n1 = ["HelloWorld", "Hapen"]print (ID (n1)) N2 = N1print (id (n2)) # # # #浅拷贝 # # # #n2 = copy.copy (n1) Print (ID (n2)) # # #深拷贝 # # #n3 = copy.deepcopy (n1) print (ID (n3))

C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe c:/homework/day3/1.py
12817912
12817912
10370784
12816672

Process finished with exit code 0

For numbers and strings, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address.

Shallow copy , creating only the first layer of data in memory

deep Copy , re-create all the data in memory (exclude the last layer, that is: Python internal optimization of strings and numbers



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.