Python Learning--Shades of copy

Source: Internet
Author: User
Tags shallow copy email account mail account

1. For numbers and strings, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address. 
>>> Import Copy
# ######### number, string #########
>>> N1 = 123
>>> Print (ID (N1))

# # # Assignment # #
>>> N2 = N1
>>> Print (ID (n2))

# # # Light Copy # #
>>> N2 = copy.copy (n1)
>>> Print (ID (n2))

# # # Deep Copy # #
>>> n3 = copy.deepcopy (N1)
>>> Print (ID (n3))

2. For dictionaries, Ganso, and lists, the changes in the memory address of the assignment, the shallow copy, and the deep copy are different.
assignment: Just create a variable that points to the original memory address
>>> n1 = {"K1": "Hu", "K2": 123, "K3": ["hh", 456]}
>>> N2 = N1

Shallow copy: Only additional first-tier data is created in memory but is worth the same address
>>> Import Copy
>>> n1 = {"K1": "Hu", "K2": 123, "K3": ["hh", 456]}
>>> N2 = copy.copy (n1)
>>> Print (ID (N1))
5706496
>>> Print (ID (n2))
6270928

>>> Print (ID (n1[' k1 '))
6204824
>>> Print (ID (n2[' k1 '))
6204824

deep Copy: Recreate all the data in memory
>>> Import Copy
>>> n1 = {"K1": "Hu", "K2": 123, "K3": ["hh", 456]}
>>> N2 = copy.deepcopy (n1)
>>> Print (ID (N1))
5706496
>>> Print (ID (n2))
6270808

>>> Print (ID (n1[' k1 '))
6204864
>>> Print (ID (n2[' k1 '))
500261040

Cases:
Dictionary dic ={' K1 ': [20,30], ' K2 ': [40,50], ' K3 ': [50,60]}, now need to update the dictionary saved in the new dictionary new_dic but does not change the original dictionary
>>> dic ={' K1 ': [20,30], ' K2 ': [40,50], ' K3 ': [50,60]}
Shallow copy:
>>> new_dic = copy.copy (DIC)
>>> new_dic[' k1 '][0] = 100
#浅拷贝改变新字典的同时也改变了原来的字典
>>> Print (New_dic)
{' K1 ': [100,30], ' K2 ': [40,50], ' K3 ': [50,60]}
>>> Print (DIC)
{' K1 ': [100,30], ' K2 ': [40,50], ' K3 ': [50,60]}

Deep Copy:
>>> new_dic = copy.copy (DIC)
>>> new_dic[' k1 '][0] = 100
#深拷贝只改变新字典
>>> Print (New_dic)
{' K1 ': [100,30], ' K2 ': [40,50], ' K3 ': [50,60]}
>>> Print (DIC)
{' K1 ': [20,30], ' K2 ': [40,50], ' K3 ': [50,60]}

python e-mail to send the source code:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 " "4 # @time: 2017/4/10 22:585 # @author: Huange6 # @version: 1.17 # @file: mail.py8 # @Software: Pycharm9 " "Ten #Coding:utf-8 #强制使用utf-8 encoded format One ImportSmtplib#loading the Smtplib module A  fromEmail.mime.textImportMimetext -  fromEmail.utilsImportformataddr -My_sender='********@163.com' #Sender e-mail account, for easy maintenance in the back, so write a variable the #my_user= ' ******* @qq. com ' #收件人邮箱账号, in order to be easy to maintain back, so write a variable - defMail (my_user):#user as formal parameter -ret=True -     Try: +Content =" " - Hello world! + SMTP (Simple Mail Transfer Protocol) is simply a message transfer protocol, which is a set of rules for sending mail from the source address to the destination. A It is the way to control the transfer of letters.  at         " " -Msg=mimetext (Content,'Plain','Utf-8') -msg[' from']=FORMATADDR (["Brother Huan", My_sender])#corresponding sender's mailbox nickname in parentheses, sender's email account -msg[' to']=FORMATADDR (["a misty rain", My_user])#corresponding recipient mailbox nickname in parentheses, Recipient's email account -msg['Subject']="python mail test" #the subject of the message, it can also be said to be a title -  inServer=smtplib. SMTP ("smtp.163.com", 25)#SMTP server in the sender's mailbox, port is -Server.login (My_sender,"Email Password")#the corresponding in parentheses is the sender's email account, email password toServer.sendmail (my_sender,[my_user,],msg.as_string ())#the corresponding in parentheses is the sender's mailbox account, the recipient's email account, the email +Server.quit ()#This is the meaning of closing the connection. -     exceptException as E:#if the statement in the try is not executed, the following ret=false is executed the         Print(e) *ret=False $     returnretPanax Notoginseng  -Ret=mail ('******* @qq. com')#actual Parameters the ifret: +     Print("OK")#If the send is successful, it will return OK, wait 20 seconds or so to receive the message A Else: the     Print("filed")#returns filed if the send fails
View Code

PS: Use 163 mailbox as a mailbox when you need to log in to 163 mailbox in Settings, open the SMTP server!

Python Learning--Shades of copy

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.