Python學習——深淺拷貝,python深淺拷貝

來源:互聯網
上載者:User

Python學習——深淺拷貝,python深淺拷貝

1.對於 數字 和 字串 而言,賦值、淺拷貝和深拷貝無意義,因為其永遠指向同一個記憶體位址。 
>>> import copy
# ######### 數字、字串 #########
>>> n1 = 123
>>> print(id(n1))

# ## 賦值 ##
>>> n2 = n1
>>> print(id(n2))

# ## 淺拷貝 ##
>>> n2 = copy.copy(n1)
>>> print(id(n2))

# ## 深拷貝 ##
>>> n3 = copy.deepcopy(n1)
>>> print(id(n3))

2.對於字典、元祖、列表 而言,進行賦值、淺拷貝和深拷貝時,其記憶體位址的變化是不同的。
賦值:只是建立一個變數,該變數指向原來記憶體位址
>>> n1 = {"k1": "hu", "k2": 123, "k3": ["hh", 456]}
>>> n2 = n1

淺拷貝:在記憶體中只額外建立第一層資料但是值得地址不變
>>> 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

深拷貝:在記憶體中將所有的資料重新建立一份
>>> 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

例:
字典dic ={'k1':[20,30],'k2':[40,50],'k3':[50,60]},現在需要更新該字典儲存在新字典new_dic 中但不改變原字典
>>> dic ={'k1':[20,30],'k2':[40,50],'k3':[50,60]}
淺拷貝:
>>> 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]}

深拷貝:
>>> 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之郵件發送源碼:
 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 ''' 4 # @time    : 2017/4/10 22:58 5 # @author  : huange 6 # @version : 1.1 7 # @file    : mail.py 8 # @Software: PyCharm 9 '''10 #coding:utf-8   #強制使用utf-8編碼格式11 import smtplib  #載入smtplib模組12 from email.mime.text import MIMEText13 from email.utils import formataddr14 my_sender='********@163.com' #寄件者郵箱帳號,為了後面易於維護,所以寫成了變數15 #my_user='*******@qq.com' #收件者郵箱帳號,為了後面易於維護,所以寫成了變數16 def mail(my_user):                  #user為形式參數17     ret=True18     try:19         content = '''20         hello world!21         SMTP(Simple Mail Transfer Protocol)即簡易郵件傳輸通訊協定,它是一組用於由源地址到目的地址傳送郵件的規則,22         由它來控制信件的中轉方式。23         '''24         msg=MIMEText(content,'plain','utf-8')25         msg['From']=formataddr(["歡哥",my_sender])   #括弧裡的對應寄件者郵箱暱稱、寄件者郵箱帳號26         msg['To']=formataddr(["一蓑煙雨",my_user])   #括弧裡的對應收件者郵箱暱稱、收件者郵箱帳號27         msg['Subject']="python郵件測試" #郵件的主題,也可以說是標題28 29         server=smtplib.SMTP("smtp.163.com",25)  #寄件者郵箱中的SMTP伺服器,連接埠是2530         server.login(my_sender,"郵箱密碼")    #括弧中對應的是寄件者郵箱帳號、郵箱密碼31         server.sendmail(my_sender,[my_user,],msg.as_string())   #括弧中對應的是寄件者郵箱帳號、收件者郵箱帳號、發送郵件32         server.quit()   #這句是關閉串連的意思33     except Exception as e:   #如果try中的語句沒有執行,則會執行下面的ret=False34         print(e)35         ret=False36     return ret37 38 ret=mail('*******@qq.com')                 #實參39 if ret:40     print("ok") #如果發送成功則會返回ok,稍等20秒左右就可以收到郵件41 else:42     print("filed")  #如果發送失敗則會返回filed
View Code

PS:用163郵箱作為發送郵箱時需要先登入163郵箱在“設定”中將SMTP伺服器開啟!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.